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的引用

posted @ 2014-02-28 20:17  木子羊羽  阅读(13106)  评论(0编辑  收藏  举报