JSON 到数据表

发布于 2024-11-01 09:16:46 字数 516 浏览 1 评论 0原文

我正在尝试将 JSON 文本序列化为 DataTable,如下所示。

List<Dictionary<string, string>> list = 
JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonText);
DataTable dTable;
dTable = (from p in list select p).CopyToDataTable();

我收到以下错误。我该如何修复它?

错误:

Cannot deserialize JSON object into type 
'System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2
[System.String,System.String]]'.

I am trying to serialize JSON text to DataTable, as in the following.

List<Dictionary<string, string>> list = 
JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonText);
DataTable dTable;
dTable = (from p in list select p).CopyToDataTable();

I get the following error. How do I fix it?

ERROR :

Cannot deserialize JSON object into type 
'System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2
[System.String,System.String]]'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的笑 2024-11-08 09:16:46

这对我有用:

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

This works for me:

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)));
书信已泛黄 2024-11-08 09:16:46
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;
    }


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


}
双手揣兜 2024-11-08 09:16:46

试试这个

public static DataTable JsonToDataTable(string Json)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = (DataTable)JsonConvert.DeserializeObject(Json, (typeof(DataTable)));
            }
            catch (Exception ex)
            {
                string chk = ex.Message;
                dt = new DataTable();
            }
            return dt;
        }

Try This

public static DataTable JsonToDataTable(string Json)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = (DataTable)JsonConvert.DeserializeObject(Json, (typeof(DataTable)));
            }
            catch (Exception ex)
            {
                string chk = ex.Message;
                dt = new DataTable();
            }
            return dt;
        }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文