.net 如何将json转换为数据表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7641004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert json into datatable?
提问by Abbas
Does anyone know how to convert a json string into DataTable from asp.net? I came to know about the Deserialize, it needs the class, I just want the datatable as returned. Can anyone tell me how to convert it to datatable?
有谁知道如何将 json 字符串从 asp.net 转换为 DataTable?我开始了解反序列化,它需要类,我只想要返回的数据表。谁能告诉我如何将其转换为数据表?
回答by Frank
Assuming that your JSON string is a list of objects, each object will correspond to a row in the DataTable, viz:
假设您的 JSON 字符串是一个对象列表,每个对象将对应于 DataTable 中的一行,即:
public DataTable DerializeDataTable()
{
const string json = @"[{""Name"":""AAA"",""Age"":""22"",""Job"":""PPP""},"
+ @"{""Name"":""BBB"",""Age"":""25"",""Job"":""QQQ""},"
+ @"{""Name"":""CCC"",""Age"":""38"",""Job"":""RRR""}]";
var table = JsonConvert.DeserializeObject<DataTable>(json);
return table;
}
This requires the Json.NET framework.
这需要Json.NET 框架。
If your JSON structure is different please post it. Best Regards.
如果您的 JSON 结构不同,请发布。此致。
回答by gourishankar
using Newtonsoft.Json;
string json = "[{"clientID":"1788","projectID":"19"},{"clientID":"1789","projectID":"24"},{"clientID":"1790","projectID":"24"},{"clientID":"1790","projectID":"23"},{"clientID":"1790","projectID":"21"}]";
DataTable tester = (DataTable) JsonConvert.DeserializeObject(json, (typeof(DataTable)));
Code for the above method
上述方法的代码
public object Deserialize(string jsonText, Type valueType)
{
try
{
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
StringReader sr = new StringReader(jsonText);
Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
object result = json.Deserialize(reader, valueType);
reader.Close();
return result;
}
catch (Exception ex)
{
throw ex;
}
}
Deserialize your jsonstring to some class
将您的 jsonstring 反序列化为某个类
List<User> UserList = JsonConvert.DeserializeObject<User>(jsonString);
Write following extension method to your project
将以下扩展方法写入您的项目
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for(int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
Call extension method like
电话扩展方法如
UserList.ToDataTable<User>();
回答by coffekid
This question is sorta outdated, but someone may be looking for answers, so here it goes. It didn't work with old JSON.NET, but today I upgraded to latest version and viola! It works great.
这个问题有点过时了,但有人可能正在寻找答案,所以就这样吧。它不适用于旧的JSON.NET,但今天我升级到最新版本和中提琴!它工作得很好。
Have serialized a DataTable into Json back and forth, zero issues! This is an awesome new feature.
已经将 DataTable 来回序列化为 Json,零问题!这是一个很棒的新功能。
Hope it helps others as well. Cheers.
希望它也能帮助其他人。干杯。
回答by James Roland
I am not sure which JSON library you are using, but you might want to take a look at JSON.NET as there is a converter object type in there called this:
我不确定您使用的是哪个 JSON 库,但您可能想看看 JSON.NET,因为那里有一个转换器对象类型,称为:
Newtonsoft.Json.Converters.DataTableConverter
// Summary:
// Converts a System.Data.DataTable to and from JSON.
I have not used this functionality before but you could have a go with it.
我以前没有使用过这个功能,但你可以尝试一下。
回答by AAP
It′s simple.
这很简单。
If you are in framework 2.0, you must import json.net (http://json.codeplex.com/) in your project, if your framework is superior, it has json.
如果你在framework 2.0,你必须在你的项目中导入json.net(http://json.codeplex.com/),如果你的framework更高级,它有json。
The code in vb.net framework 2.0:
vb.net framework 2.0 中的代码:
Dim Table DataTable
Table = Json.JsonConvert.DeserializeObject(Of DataTable)(Json_string)
That′s all.
就这样。

