【发布时间】:2012-06-17 13:51:39
【问题描述】:
我收到了一个 JSON 文件,其中包含一个根元素“用户”和一个“用户”项目列表。
我正在尝试将 json 反序列化为名为 User 的自定义类的 List,但我不断收到 JsonSerializationException,它无法覆盖它。
我尝试了以下方法:
代码:
public class User
{
public int ID { get; set; }
public bool Active { get; set; }
public string Name { get; set; }
}
public class Response
{
public List<User> Users { get; set; }
public JObject Exception { get; set; }
}
还有-
public Response DeserializeJSON(string json)
{
Response deserialized = JsonConvert.DeserializeObject<Response>(json);
return deserialized;
}
JSON:
{
"Users": {
"User": [
{
"id": "1",
"active": "true",
"name": "Avi"
},
{
"id": "2",
"active": "false",
"name": "Shira"
},
{
"id": "3",
"active": "false",
"name": "Moshe"
},
{
"id": "4",
"active": "false",
"name": "Kobi"
},
{
"id": "5",
"active": "true",
"name": "Yael"
}
]
}
}
抱歉造型不好!!
【问题讨论】:
-
您确定 JSON 是有效的吗?尝试将其转储到 jsonlint.com 并查看 JSON 本身没有问题。
-
这是我做的第一件事 :) 确实有效。
标签: json serialization json.net root