2011-08-24 54 views
1

我是從一個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鍵是搞砸了,但我也試過了一個字符串數組。

+0

屬性名稱應該有第一個字母大寫。請參閱「有效」和「接收」 – DenisPostu

回答

5

我的測試代碼不會與嵌套的Info類一起編譯(由於屬性命名衝突),因此我從ResposeEntity類中刪除了它。

與此一起,我修復了一些與您的JSON有關的問題(您的信息對象是一個數組,並且錯誤數組中的字符串需要用引號引起來)。

見下圖:

JSON

{ 
    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; } 
    } 
} 

測試代碼

string json = "{\"info\":{\"calls\":0,\"errors\":[\"error1\", \"error2\", \"error3\"],\"messages\":0,\"mail\":3},\"received\":5,\"valid\":3}"; 

ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity; 

希望這會有所幫助。

+0

謝謝,我沒有想到這個問題會與實際的JSON,我沒有看得很近。我已聯繫提供商向我發送數據,他們正在修復它。 – Ryan

相關問題