如何将JSON转换为数据表?

问题描述 投票:0回答:5

有谁知道如何在 ASP.NET 中将 JSON 字符串转换为 DataTable 吗?我了解反序列化;我只想返回数据表。谁能告诉我如何将其转换为数据表?

.net datatable json.net deserialization
5个回答
22
投票

假设你的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;
}

这需要Newtonsoft的Json.NET框架


11
投票
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)));

上述方法的代码

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;
    }


}

将 jsonstring 反序列化为某个类

List<User> UserList = JsonConvert.DeserializeObject<User>(jsonString);

将以下扩展方法写入您的项目

 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;
}

调用扩展方法如

UserList.ToDataTable<User>();

2
投票

这个问题有点过时了,但可能有人正在寻找答案,所以就在这里。 它不适用于旧的 JSON.NET,但今天我升级到最新版本和 viola!效果很好。

已将 DataTable 来回序列化为 Json,零问题!这是一个很棒的新功能。


1
投票

我不确定您正在使用哪个 JSON 库,但您可能想看看 JSON.NET,因为其中有一个名为以下的转换器对象类型:

 Newtonsoft.Json.Converters.DataTableConverter
    // Summary:
    //     Converts a System.Data.DataTable to and from JSON.

我以前没有使用过此功能,但你可以尝试一下。


0
投票

如果你的框架是2.0,你的项目中必须导入Json.net(http://json.codeplex.com/),如果你的框架更高级,它有JSON。

vb.net框架2.0中的代码:

Dim Table DataTable

Table = Json.JsonConvert.DeserializeObject(Of DataTable)(Json_string)
© www.soinside.com 2019 - 2024. All rights reserved.