2015-04-02 68 views
0

我從zoho獲得json。我有這樣一個JSON如下:Json反序列化沒有結果

{ 
    "response": { 
    "result": { 
     "Leads": { 
     "row": [ 
      { 
      "no": "1", 
      "FL": [ 
       { 
       "content": "1325469000000679001", 
       "val": "LEADID" 
       }, 
       { 
       "content": "1325469000000075001", 
       "val": "SMOWNERID" 
       }, 
       { 
       "content": "Geoff", 
       "val": "Lead Owner" 
       }, 
      ] 
      }, 
      { 
      "no": "2", 
      "FL": [ 
       { 
       "content": "1325469000000659017", 
       "val": "LEADID" 
       }, 
       { 
       "content": "1325469000000075001", 
       "val": "SMOWNERID" 
       }, 
       { 
       "content": "Geoff", 
       "val": "Lead Owner" 
       }, 
      ] 
      }, 

     ] 
     } 
    }, 
    "uri": "/crm/private/json/Leads/getRecords" 
    } 
} 

我使用以下類:

public class Row 
{ 

    [JsonProperty(PropertyName = "row")] 
    public List<Leads> row { get; set; } 

} 

public class Leads 
{ 
    [JsonProperty(PropertyName = "no")] 
    public string nbr { get; set; } 

    [JsonProperty(PropertyName = "FL")] 
    public List<Lead> FieldValues { get; set; } 

} 

public class Lead 
{ 

    [JsonProperty(PropertyName = "content")] 
    public string Content { get; set; } 

    [JsonProperty(PropertyName = "val")] 
    public string Val { get; set; } 

} 

我嘗試反序列化JSON,並取回什麼:

var mList = JsonConvert.DeserializeObject<IDictionary<string, Row>>(result); 

這是第一次與Json合作,所以任何幫助將不勝感激!

+0

http://www.newtonsoft.com/json/help/html/SerializationAttributes.htm – 2015-04-02 20:26:15

+0

使用剪貼板上的JSON字符串,您可以執行**編輯 - >選擇性粘貼 - >粘貼JSON作爲類**和VS會爲你創建JSON的類 – Plutonix 2015-04-02 20:42:56

回答

3

通常發生這種情況是因爲您的反序列化類模型是錯誤的。而不是試圖手工製作我喜歡使用的課程http://json2csharp.com。只需插入您的JSON,它就會爲您提供必要的C#類。在你的情況下,它提供了以下內容。

public class FL 
{ 
    public string content { get; set; } 
    public string val { get; set; } 
} 

public class Row 
{ 
    public string no { get; set; } 
    public List<FL> FL { get; set; } 
} 

public class Leads 
{ 
    public List<Row> row { get; set; } 
} 

public class Result 
{ 
    public Leads Leads { get; set; } 
} 

public class Response 
{ 
    public Result result { get; set; } 
    public string uri { get; set; } 
} 

public class RootObject 
{ 
    public Response response { get; set; } 
} 

然後,您可以反序列化爲RootObject使用:

var mList = JsonConvert.DeserializeObject<RootObject>(result); 

隨意命名RootObject以任何名義,你更喜歡。

+0

這很棒!謝謝! – 2015-04-02 20:05:58