我收到了一個JSON文件,其中包含一個根元素「users」和一個「user」項列表。使用Json.NET對自定義對象數組反序列化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是有效的嗎?嘗試在http://jsonlint.com/這裏傾倒它,看看它在JSON本身沒有一些問題。 –
這是我做的第一件事:)它的確是有效的。 – user1461793