简体   繁体   中英

How to convert JSON array into object list in the c#

I have a json object as below and I wants to convert it into

[
    {
        "Id": 1114,
        "ParentId": 45333,
        "IsActive": true,
        "Name": "John",
        "Contact": "123456"           
    },
    {
        "Id": 11344,
        "ParentId": 54434,
        "IsActive": false,
        "Name": "Levi",
        "Contact": "53552333"
    },
    {
        "Id": 124433,
        "ParentId": 535233,
        "IsActive": false,
        "Name": "Larry",
        "Contact": "5443554"
    }

  ]

}

I have tried below option but I am getting error "No parameterless constructor defined for type of 'MyApp.Emp[]'."

JavaScriptSerializer js = new JavaScriptSerializer();
        Emp[] acc = js.Deserialize<Emp[]>(json);

Below is my Emp class

 public class Emp
{
    public Emp()
    { 

    }

    public int Id { get; set; }
    public int ParentId { get; set; }
    public bool IsActive { get; set; }
    public string Name { get; set; }
    public int Contact { get; set; }
}

Can anyone please show me how I can do it successfully.

Thanks

hopefully this answer is your want

PS : output

public JsonResult SaveResult()
{    
        return Json(new { err = "***"});    
}   

PS: input

public JsonResult ReadResult()
        {
            Object1 xxx = new Object1();
            Object2 aa = new Object2();
            xxx.x2.Add(aa);
            aa = new Object2();
            aa.y = "200";
            aa.z = "222";
            xxx.x2.Add(aa);
            var json = JsonConvert.SerializeObject(xxx, Formatting.None);


            return Json(json);
        }


        public class Object1
        {
            public string x1 = "aaaaa";
            public IList<Object2> x2 = new List<Object2>();
        }
        public class Object2
        {
            public string y = "100";
            public string z = "10";
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM