我是從一個http請求,看起來像這樣接收JSON字符串回:json.net反序列化字符串嵌套類
{
"info":
[
{
"calls":0,
"errors":"[error1, error2, error3]",
"messages":0,
"mail":3
}
],
"received":5,
"valid":3
}
我試圖反序列化到實體結構大致相同
class ResponseEntity
{
private Info info;
private int received;
private int valid;
[JsonProperty("info")]
public Info Info
{
get { return info; }
set { info = value; }
}
[JsonProperty("valid")]
public int valid
{
get { return valid; }
set { valid = value; }
}
[JsonProperty("received")]
public int received
{
get { return received; }
set { received = value; }
}
public class Info
{
private int calls;
private List<string> errors;
private int messages;
private int mail;
[JsonProperty("calls")]
public int Calls
{
get { return calls; }
set { calls = value; }
}
[JsonProperty("messages")]
public int Messages
{
get { return messages; }
set { messages = value; }
}
[JsonProperty("errors")]
public List<string> Errors
{
get { return errors; }
set { errors = value; }
}
[JsonProperty("mail")]
public int Mail
{
get { return mail; }
set { mail = value; }
}
}
}
當我嘗試反序列化,雖然我得到一個異常
ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity;
Cannot deserialize JSON array into type 'CSharpRestService.ResponseEntity+Info'.
任何人可以看看我做錯了什麼?我在想'錯誤'json鍵是搞砸了,但我也試過了一個字符串數組。
屬性名稱應該有第一個字母大寫。請參閱「有效」和「接收」 – DenisPostu