使用c#将JSON字符串反序列化为DataTable / DataSet

时间:2014-11-17 10:40:53

标签: c# json json.net

我有以下JSON字符串

{
"1":[
        {"cityId":93,"cityName":"Tapah","cityCode":"TAP"},
        {"cityId":3,"cityName":"Melaka","cityCode":"MLK"},
        {"cityId":6,"cityName":"Kota Bharu","cityCode":"KB"},
        {"cityId":7,"cityName":"Dungun","cityCode":"DG"}
    ],
"2":[
        {"cityId":77,"cityName":"Grik","cityCode":"GRIK"},
        {"cityId":6,"cityName":"Kota Bharu","cityCode":"KB"},
        {"cityId":7,"cityName":"Dungun","cityCode":"DG"},
        {"cityId":98,"cityName":"Bangan Serai","cityCode":"BS"}
    ],
"6":[
        {"cityId":3,"cityName":"Melaka","cityCode":"MLK"},
        {"cityId":82,"cityName":"Teluk Intan","cityCode":"TI"},
        {"cityId":7,"cityName":"Dungun","cityCode":"DG"}
    ]
}

当我使用JSON.NET时

  

DataSet obj = JsonConvert.DeserializeObject(responseBody);

我只接受以下表格

cityId  |  cityName   | cityCode
-----------------------------------
93           Tapah          TAP
3            Melaka         MLK
6            Kota Bharu     KB
7            Dungun         DG

我需要喜欢这个

from  |    cityId  |  cityName   | cityCode
-----------------------------------------------
 1            93           Tapah          TAP
 1            3            Melaka         MLK
 1            6            Kota Bharu     KB
 1            7            Dungun         DG
 2            77           Grik           GRIK
 2            6            Kota Bharu     KB
 2            7            Dungun         DG
 2            98           Bangan Serai   BS
 6            3            Melaka         MLK
 6            82           Teluk Intan    TI
 6            7            Dungun         DG             

2 个答案:

答案 0 :(得分:1)

问题的原因是JsonConvert.DeserializeObject的错误使用在这种情况下应该起作用:

使用列详细信息定义类型/模式(如上面的响应所示)

public class City
{
    public int cityId;
    public string cityName;
    public string cityCode;
}

现在你在响应体中获得的是类型,Json是默认的键值对

Dictionary<object, List<City>>

现在你的反序列化代码应该是:

var obj = JsonConvert.DeserializeObject<Dictionary<object, List<City>>>(responseBody);

obj属于Dictionary<object, List<City>>

类型

现在创建自定义数据集对象的模式,如下所示:

public class CustomDataset
    {
        public object from;
        public int cityId;
        public string cityName;
        public string cityCode;
    }

使用简单的foreach从obj类型创建自定义数据集,如下所示:

List<CustomDataset> cdList = new List<CustomDataset> ();

foreach(object key in obj.Keys)
{
foreach(City c in obj[key])
{
  cdList.Add(key, c.cityId, c.cityName, cityCode);
}
}

类型List<CustomDataset>的cdList将是您需要的返回包含解决方案。如果你使用Linq,可以跳过使用Foreach的最终部分,这很简单,如下所示:

List<CustomDataset> cdList = 
                              obj.SelectMany(x => x.Value.Select(y =>
                                               new CustomDataset
                                               {
                                                from = x.Key,
                                                cityId = y.cityId,
                                                cityName = y.cityName,
                                                cityCode =  y.cityCode
                                               }
                                               )).ToList();

答案 1 :(得分:0)

我建议将JSON对象反序列化为语义上等效的框架类型。在您的情况下,JSON在语义上等同于IDictionary<TKey, ICollection<TValue>>>

IDictionary<int, ICollection<City>> cities;
cities = JsonConvert.DeserializeObject<IDictionary<int, ICollection<City>>>(responseBody);

然后,自己进行DataSet转换,而不是依赖于反射。

var dataSet = new DataSet();
var dataTable = dataSet.Tables.Add();
dataTable.Columns.Add("from");
dataTable.Columns.Add("cityId");
dataTable.Columns.Add("cityName");
dataTable.Columns.Add("cityCode");

// foreach KeyValuePair in IDictionary<TKey, ICollection<TValue>>
foreach (var cityGroup in cities)
{
    // foreach TValue in ICollection<TValue>
    foreach (var city in cityGroup.Value)
    {
        dataTable.LoadDataRow(
            new object[]
            {
                cityGroup.Key,
                city.cityId,
                city.cityName,
                city.cityCode
            },
            LoadOption.PreserveChanges);
    }
}

对于TValue,我使用了一个简单的数据合约类。

public class City
{
    public int cityId;
    public string cityName;
    public string cityCode;
}