2014-08-28 54 views
0

我有一個JSON字符串,我需要創建C#類,然後以類似的格式解析整個List。 JSON字符串包含「0」和「1」。我已註解類的屬性與編寫自定義c#類來表示JSON?

[JsonProperty( 「0」)]

但看起來像它不工作。

{ 
    "draw": 4, 
    "recordsTotal": 57, 
    "recordsFiltered": 57, 
    "data": [ 
    { 
     "0": "Charde", 
     "1": "Marshall", 
     "2": "Regional Director", 
     "3": "San Francisco", 
     "4": "16th Oct 08", 
     "5": "$470,600", 
     "DT_RowId": "row_13" 
    }, 
    { 
     "0": "Colleen", 
     "1": "Hurst", 
     "2": "Javascript Developer", 
     "3": "San Francisco", 
     "4": "15th Sep 09", 
     "5": "$205,500", 
     "DT_RowId": "row_9" 
    }, 
    { 
     "0": "Dai", 
     "1": "Rios", 
     "2": "Personnel Lead", 
     "3": "Edinburgh", 
     "4": "26th Sep 12", 
     "5": "$217,500", 
     "DT_RowId": "row_20" 
    }] 
    } 

類,我已經嘗試了這個JSON

public class UserData 
    { 
     [JsonProperty("0")] 
     public string Name { get; set; } 

     [JsonProperty("1")] 
     public string Email { get; set; } 

     //Having more JSON properites 

     [JsonProperty("DT_RowId")] 
     public long UserId { get; set; } 
    } 

    public class JsonValdate 
    { 
     public string draw { get; set; } 
     public int length { get; set; } 
     public int start { get; set; } 
     public int recordsFiltered { get; set; } 
     public int recordsTotal { get; set; } 

     [JsonProperty("data")] 
     public UserData[] data { get; set; } 
    } 

回答

1

這可能是一些JSON數據的不能轉換爲的UserData屬性

馬上關閉吧,你可以看到"DT_RowId": "row_20"不能轉換爲long UserId

使用嘗試catch塊轉換之外,並看到異常。

例如,

private string Json 
{ 
    get { 
     return @" 
     { 
      ""draw"": 4, 
      ... 
     }"; 
    } 
} 

try 
{ 
    JsonValdate result = JsonConvert.DeserializeObject<JsonValdate>(Json); 
} 
catch (Exception ex) 
{ 
    // Debug 
} 

這裏是我如何測試

由於轉換不工作,不要把所有領域的一次。

從下列工作領域開始。然後一次添加一個字段。

private string Json 
{ 
    get { return @" 
     { 
     ""draw"": 4, 
     ""recordsTotal"": 57, 
     ""recordsFiltered"": 57, 
     ""data"": [ 
       { 
        ""0"": ""Charde"", 
        ""1"": ""Marshall"" 
       }, 
       { 
        ""0"": ""Colleen"", 
        ""1"": ""Hurst"" 
       }, 
       { 
        ""0"": ""Dai"", 
        ""1"": ""Rios"" 
       }] 
     }"; 
    } 
} 

public class UserData 
{ 
    [JsonProperty("0")] 
    public string Name { get; set; } 

    [JsonProperty("1")] 
    public string Email { get; set; } 
} 

public class JsonValdate 
{ 
    public string draw { get; set; } 
    public int length { get; set; } 
    public int start { get; set; } 
    public int recordsFiltered { get; set; } 
    public int recordsTotal { get; set; } 

    [JsonProperty("data")] 
    public UserData[] data { get; set; } 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    try 
    { 
     JsonValdate result = JsonConvert.DeserializeObject<JsonValdate>(Json); 
    } 
    catch (Exception ex) 
    { 

    } 
} 
+0

我已經將UserId long轉換爲字符串,而Json轉換。它工作正常。數據數組索引中沒有「0」和「1」。 thx – user223372 2014-08-28 17:04:59

+0

我更新瞭如何調試的答案。 – Win 2014-08-28 17:14:14