【发布时间】: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));
它可以工作,但不确定如何访问每个对象或值并转换为数据表
【问题讨论】:
-
谷歌它或检查these答案。
标签: .net