所以,这是我的班级:
public class User
{
public User() { }
public string Username { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
这就是我的JSON-String的样子:
{"results":[{"FirstName":"firstname1","LastName":"lastname1","Password":"TestPassword","Username":"TestUser","createdAt":"2015-03-02T17:36:25.232Z","objectId":"a8bKXDg2Y2","updatedAt":"2015-03-02T20:35:48.755Z"},{"FirstName":"firstname2","LastName":"lastname2","Password":"TestPw","Username":"TestUser2","createdAt":"2015-03-02T20:35:26.604Z","objectId":"2XPIklE3uW","updatedAt":"2015-03-02T20:35:53.712Z"}]}
我想让一个User []用户离开它。 我的问题是{"结果:":[....]} - Part。
我已经这样试过了:
JavaScriptSerializer js = new JavaScriptSerializer();
User[] user = js.Deserialize<User[]>(jsonString);
但我认为结果 - 部分是以某种方式弄乱一切。 什么是解决这个问题的最佳方法?
答案 0 :(得分:7)
尝试定义一个反映您的JSON结构的包装模型:
public class MyModel
{
public User[] Results { get; set; }
}
现在您可以继续将JSON字符串反序列化回此包装模型:
JavaScriptSerializer js = new JavaScriptSerializer();
MyModel model = js.Deserialize<MyModel>(jsonString);
现在没有任何东西阻止您收回用户集合:
User[] user = model.Results;
答案 1 :(得分:1)
您的模型需要另一个图层
public class Data
{
public User[] results { get; set; }
}
然后反序列化Data
类