【问题标题】:How can I convert JSON to DataTable using C#如何使用 C# 将 JSON 转换为 DataTable
【发布时间】:2011-12-13 13:15:24
【问题描述】:

我有一个 json 字符串,想从中创建一个 DataTable。

如何在 C# 中将 JSON 转换为 DataTable?

更新:

我已经按照此处提供的链接使用了 Json.Net

并构建 2 个类来处理 json 字符串,如下所示

 public class JsonHelper
        {
            public List<User> userdata { get; set; }
        }

  public class User
    {
        public string name { get; set; }
        public string id { get; set; }
        public DateTime createdDate { get; set; }

    }

并使用以下代码反序列化

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(jsonstr);
            Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
            object result = json.Deserialize(reader, typeof( JsonHelper));
            reader.Close();

            return result;

但出现以下错误

无法将 JSON 数组反序列化为类型“mynamespace+JsonHelper”。

这里应该是什么问题,请帮我解决这个问题。

谢谢。

【问题讨论】:

    标签: c# json serialization json.net


    【解决方案1】:

    Rick Strahl 的This post 可以帮助您。在幕后,他使用 Newtonsoft 的 JSON.NET 库来完成繁重的工作。

    【讨论】:

    • 我曾尝试使用此功能,但出现错误,请参阅我更新后的问题。
    猜你喜欢
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多