将反序列化的json类转换为datatable [英] Convert deserialized json class to datatable

查看:243
本文介绍了将反序列化的json类转换为datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ p $

我试图通过使用下面的代码将我反序列化为类的json转换为数据类型,但是下面的代码在最后一行失败。 p> using(var webClient = new System.Net.WebClient())
{
var downloadTable = webClient.DownloadString(url);
var myTable = JsonConvert.DeserializeObject< leagueTable>(downloadTable);
DataTable dt = myTable;
}



我知道我可以反序列化为一个datatable,但我想反序列化首先,我可以操纵数据,并让数据的结构知道使用代码的其他人。



JSON是嵌套的,

  public class leagueTable 
{
public string leaguename {get;组; }
public int gameday {get;组; }
public System.Collections.ObjectModel.Collection< Ranking>排名{get;组; }
}

public class Ranking
{
public int rank {get;组; }
public string club {get;组; }
public int games {get;组; }
public int points {get;组; }
}


解决方案

被反序列化成一个DataTable,它需要一个json对象的数组,其中数组中的每个第一级对象都对应一行。
例如,这将工作正常:

  string data =[{\FirstName \ \John \,\LastName \:\Smith\},{\FirstName \:\Mohammed \,\LastName \ \Lee\}]; 
var dt = JsonConvert.DeserializeObject< DataTable>(data);

请注意,整个json字符串在 [] 。即使它是一个仅包含数组的json对象,它也不会工作。



如果你想反序列化为自定义类型,那么我建议你反序列化相同的json转换为列表< T> 而不是数据表。

  class Name 
{
public string FirstName {get;组; }
public string LastName {get;组; }
}

...

var names = JsonConvert.DeserializeObject< List< Name>

然后有将列表转换为DataTable的方法。话虽如此,一旦你反序列化为自定义列表类型,你仍然想把它转换成一个DataTable?如果DataTable用于控件上的数据绑定,还有其他绑定源的选项。


I am trying to convert json that I deserialized to a class to a datatable by using the code below ,however the code below fails at the last line.

using (var webClient = new System.Net.WebClient())
{
    var downloadTable = webClient.DownloadString(url);
    var myTable = JsonConvert.DeserializeObject<leagueTable>(downloadTable);
    DataTable dt = myTable;
}

I know that I could deserialized directly to a datatable but I want to deserialized it to a class first so that I can manipulate the data and let the structure of the data be known to others that use the code.

The JSON is nested and the classes made for it is below

public class leagueTable
{
    public string leaguename { get; set; }
    public int gameday { get; set; }
    public System.Collections.ObjectModel.Collection<Ranking> ranking { get; set; }
}

public class Ranking
{
    public int rank { get; set; }
    public string club { get; set; }
    public int games { get; set; }
    public int points { get; set; }
}

解决方案

For your json string to get deserialized into a DataTable, it needs to be an array of json objects, where each first-level object in the array corresponds to a row. For instance, this would work fine:

string data = "[{\"FirstName\": \"John\", \"LastName\": \"Smith\"}, {\"FirstName\": \"Mohammed\", \"LastName\": \"Lee\"}]"; 
var dt = JsonConvert.DeserializeObject<DataTable>(data);

Notice that that whole json string is within a []. It wont work even if it is a json object containing only an array.

If you want to deserialize into a custom type first, then I would suggest that you deserialize the exact same json into a List<T> instead of a DataTable.

class Name 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

...

var names = JsonConvert.DeserializeObject<List<Name>>(data);

And then there are ways to convert the list to a DataTable. Having said that, once you have deserialized into a custom list type, would you still want to convert that into a DataTable? If the DataTable is to be used for data binding on controls, there are other options for binding sources.

这篇关于将反序列化的json类转换为datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆