2016-04-19 33 views
0

我從API中拉取數據(不是我的,我無法更改其格式化的方式),然後將其映射到C#列表中,從而遇到了問題。C# - 將JSON值映射到以數字作爲ID的列表

的JSON的小樣品看起來像這樣(有上千個form_values部分記錄和數千條記錄)

JSON示例:

{ 
    "records":[ 
     { 
     "status":"4", 
     "version":5, 
     "form_values":{ 
      "4015":"TextValue", 
      "5919":"TextValue", 
      "6127":"TextValue", 
      "7868":"0", 
      "q311":"TextValue", 
      "r83b":"0" 
     } 
     } 
    ], 
    "current_page":1, 
    "total_pages":1, 
    "total_count":1, 
    "per_page":12 
} 

目前我把將JSON結果轉換爲強類型列表,其代碼如下(This uses Newtonsoft.Json)

C#JSON反序列化到列表

JsonConvert.DeserializeObject<Data>(json_data); 

正是在Data.cs,我有問題,因爲C#不會允許一個變量開始與INT類。

我試過了,沒有成功,就是使用Runtime.Serilization DataMember屬性。

Data.cs

 [DataMember(Name = "4015")] 
     public string textfeild { get; set; } 

的問題:

這導致一個空值被設置而不管數據的後面。所有以字母開頭的字段都可以正常工作,所以我相信我的JSON正確地寫入列表中,只需要一個解決這些令人討厭的ID以數字開頭的解決方案!

任何幫助將不勝感激。

+0

也許參考這可以幫助你:HTTP://計算器。com/questions/1207731/how-can-i-deserialize -json-to-a-simple-dictionarystring-string-in-asp-net – rinukkusu

+0

試試這個工具:https://jsonclassgenerator.codeplex.com/與你的JSON生成C#類,然後嘗試反序列化。 – Rocker1985

回答

2

你有兩個選擇,第一個是使用JsonProperty屬性:

public class FormValues 
{ 
    [JsonProperty(PropertyName = "4015")] 
    public string T4015 { get; set; } 
    [JsonProperty(PropertyName = "5919")] 
    public string T5919 { get; set; } 
    [JsonProperty(PropertyName = "6127")] 
    public string T6127 { get; set; } 
    [JsonProperty(PropertyName = "7868")] 
    public string T7868 { get; set; } 
    public string q311 { get; set; } 
    public string r83b { get; set; } 
} 

public class Record 
{ 
    public string status { get; set; } 
    public int version { get; set; } 
    public FormValues form_values { get; set; } 
} 

public class Data 
{ 
    public List<Record> records { get; set; } 
    public int current_page { get; set; } 
    public int total_pages { get; set; } 
    public int total_count { get; set; } 
    public int per_page { get; set; } 
} 

第二個是使用Dictionary<string, string>form_values物業編號:

public class Record 
{ 
    public string status { get; set; } 
    public int version { get; set; } 
    public Dictionary<string, string> form_values { get; set; } 
} 

public class Data 
{ 
    public List<Record> records { get; set; } 
    public int current_page { get; set; } 
    public int total_pages { get; set; } 
    public int total_count { get; set; } 
    public int per_page { get; set; } 
} 

如果你的form_values不是固定的,那麼你應該堅持第二種選擇。

+0

JsonProperty(PropertyName)是一種享受 – Caz1224

1

form_values創建一個屬性作爲字典

public Dictionary<string, string> form_values { get; set; } 
0
Use List<KeyValuePair<string, string>> 

var dictionary = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(dict) 
          .ToDictionary(x => x.Key, y => y.Value); 

使用能代表您對自定義對象,然後創建一個從您的收藏字典。

var output = JsonConvert.DeserializeObject<List<Temp>>(dict); 
      var dictionary = output.ToDictionary(x => x.Key, y => y.Value); 

      public class Temp 
      { 
       public string Key { get; set; } 
       public string Value { get; set; } 
      } 

你也可以得到Here