C# Json格式转换成List集合

public List<User> JsonToList(string str)
{
str = "{\"User\":[{\"Name\":\"Sony\",\"Age\":7},{\"Name\":\"Samsumg\",\"Age\":14},{\"Name\":\"LG\",\"Age\":21},{\"Name\":\"Vizio\",\"Age\":34},{\"Name\":\"Insignia\",\"Age\":24}]}";
JavaScriptSerializer json = new JavaScriptSerializer();
var list = json.DeserializeObject(str) as IDictionary<string,object>;
var groups =( from s in (IEnumerable<object>)list["User"]
let i = s as IDictionary<string, object>
where i != null
select new User()
{
Name = (string)i["Name"],
Age = (int)i["Age"]
}).ToList();
return groups;
}

 

public class User
{
public string Name { get; set; }
public int Age { get; set; }
}

 

 

要添加System.Web.Extensions.dll的引用

C# Json格式转换成List集合,布布扣,bubuko.com

C# Json格式转换成List集合

上一篇:DataWhale八月组队学习-李宏毅深度学习Task05-网络设计的技巧


下一篇:HTML前端技术(JS的使用,包括数组和字符串)