c# - 将嵌套的 JSON 数据解析为 DataTable

标签 c# json datatable

我需要解析下面嵌套到数据表中的一些 JSON 数据。 3 个组中每个组的“属性”字段都包含标题、值和优先级的内部数据。

[{
"Title": "OVERVIEW",
"Priority": 1,
"attributes": [{
    "Title": "Type",
    "Value": "MacBook Pro",
    "Priority": 1
    },
    {
       "Title": "Operating system",
       "Value": "OS X Mountain Lion",
       "Priority": 2
    },
    {
       "Title": "Processor",
       "Value": "Intel Core i5 Processor (2.3 GHz, 3.3 GHz with TurboBoost, 6MB cache)",
       "Priority": 3
   },
   {
       "Title": "Storage",
       "Value": "500 GB HDDM 5400 rpm",
       "Priority": 4
   }]
},
{
"Title": "SPECIFICATION",
"Priority": 2,
"attributes": [{
    "Title": "RAM",
    "Value": "4 GB DDR3",
    "Priority": 1
    }]
},
{
"Title": "SCREEN",
"Priority": 3,
"attributes": [{
    "Title": "Screen size",
    "Value": "13\"",
    "Priority": 1
    }]
}]

我意识到我需要先反序列化 JSON 数据,

List<User> UserList = JsonConvert.DeserializeObject<List<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;        
}

但不确定从那里去哪里,因为上面只考虑了正常的 JSON 数据。任何帮助将不胜感激。

最佳答案

使用这个类反序列化数据

         public class TitleDesc
    {
        public string Title { get; set; }
        public int Priority { get; set; }
        public Attribute[] attributes { get; set; }
    }

    public class Attribute
    {
        public string Title { get; set; }
        public string Value { get; set; }
        public int Priority { get; set; }
    }

然后用这段代码序列化

string jsonString = "[{'Title': 'OVERVIEW','Priority': 1,'attributes': [{    'Title': 'Type',    'Value': 'MacBook Pro',    'Priority': 1    },    {       'Title': 'Operating system',       'Value': 'OS X Mountain Lion',       'Priority': 2    },    {       'Title': 'Processor',       'Value': 'Intel Core i5 Processor (2.3 GHz, 3.3 GHz with TurboBoost, 6MB cache)',       'Priority': 3   },   {       'Title': 'Storage',       'Value': '500 GB HDDM 5400 rpm',       'Priority': 4   }]},{'Title': 'SPECIFICATION','Priority': 2,'attributes': [{    'Title': 'RAM',    'Value': '4 GB DDR3',    'Priority': 1    }]},{'Title': 'SCREEN','Priority': 3,'attributes': [{    'Title': 'Screen size',    'Value': '13',    'Priority': 1    }]}]";
        var data = JsonConvert.DeserializeObject<TitleDesc[]>(jsonString);

希望这对您有所帮助。

关于c# - 将嵌套的 JSON 数据解析为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28450415/

相关文章:

ios - 循环遍历 SwiftyJSON

javascript - jQuery DataTable 中的分页不起作用

r - 根据 R 中的某些条件计算值?

c# - Visual Studio 扩展包中未触发 DebuggerEvents.OnEnterBreakMode

c# - 将依赖的可观察值映射到子列表

javascript - 来自 CSV 的 DS.js JSON 文件格式

python - python 2.7 中来自 googlefinance 的 HTTP 错误 404

c# - 检查是否没有返回数据行

c# - 使用 Wix# 构建 MSI 的递归 DirFiles

c# - 我的 Visual Studio Code 中的区域折叠不起作用