Adam
Adam

Reputation: 45

create an object inside an array JSON c#

anybody know how to achieve that with a c# code

"cards": {
      "en-US": [{
              "sys": {
                "type": "Link",
                "linkType": "Entry",
                "id": "id"
              }
      } ]
  }

this is my coded

Cards = new Dictionary<string, List<Sys>>()
            {
                { "en-US" , new List<Sys>{ new Sys { id = "7Ll4cezI6QMYsgIUwMCmSw", type = "Link", linkType = "Entry" } } }
            }

and a sys class

public class Sys
{

    public string id { get; set; }
    public string type { get; set; }
    public string linkType { get; set; }


}

but what im acheiving here is

"Cards":{"en-US":[{"id":"id","type":"Link","linkType":"Entry"}]}

and what i want is the "sys" that is not coming , any help

Upvotes: 0

Views: 92

Answers (1)

bruno.almeida
bruno.almeida

Reputation: 2896

Try this:

    public class SysParent
    {
        public Sys sys { get; set; }
    }

    public class Sys
    {

        public string id { get; set; }
        public string type { get; set; }
        public string linkType { get; set; }


    }

        var Cards = new Dictionary<string, List<SysParent>>()
        {
            { "en-US" , new List<SysParent>{ new SysParent{
                sys = new Sys { id = "7Ll4cezI6QMYsgIUwMCmSw", type = "Link", linkType = "Entry" } }
            } }
        };

This way you add a parent object with name sys.

Upvotes: 1

Related Questions