2016-04-19 34 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