如何将JSON数组转换为c#中的对象列表

时间:2015-09-09 15:51:34

标签: c# json asp.net-web-api

我有一个json对象如下,我想将其转换为

[
    {
        "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"
    }

  ]

}

我已尝试过以下选项,但我收到错误“没有为'MyApp.Emp []'类型定义无参数构造函数。”

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

以下是我的Emp课程

 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; }
}

任何人都可以告诉我如何成功地做到这一点。

由于

1 个答案:

答案 0 :(得分:0)

希望这个答案是你想要的

PS:输出

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

PS:输入

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";
        }