【问题标题】:How to convert my JSON to DataTable?如何将我的 JSON 转换为 DataTable?
【发布时间】:2016-06-16 06:14:23
【问题描述】:

我有下面的 JSON,我想将其转换为 Datatable,我尝试了下面的代码,但它出错了。有人可以帮我解决这个问题吗

[
        {
            "Attributes": [
                {
                    "ProductId": 100,
                    "AttributeCode": "Code",
                    "AttributeValue": "xyz"
                },
                {
                    "ProductId": 100,
                    "AttributeCode": "PID",
                    "AttributeValue": "71"
                },
                {
                    "ProductId": 100,
                    "AttributeCode": "STATUS",
                    "AttributeValue": "Active"
                },
                {
                    "ProductId": 100,
                    "AttributeCode": "Type",
                    "AttributeValue": "Offering"
                }
            ],
            "MasterId": 100,
            "ProductName": "Core Credit Services"
        }
    ]

string path = @"c:\data\test.txt";

            if (!File.Exists(path))
            {
                MessageBox.Show("File does not exist");
                return;
            }

            string json = File.ReadAllText(path);

            JArray j = JsonConvert.DeserializeObject<JArray>(json);

            RootObject rt = JsonConvert.DeserializeObject<RootObject>(json);

当我尝试使用时

dynamic obj = JsonConvert.DeserializeObject(json, typeof(object));

它可以工作,但不确定如何访问每个对象或值并转换为数据表

【问题讨论】:

标签: .net


【解决方案1】:

没关系,我尝试了以下行并且它有效。希望它会帮助别人

List<RootObject> rt = JsonConvert.DeserializeObject<List<RootObject>>(json);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2015-10-01
    相关资源
    最近更新 更多