【发布时间】:2016-08-27 05:45:27
【问题描述】:
我有以下 c# 类
public class Output
{
public string name { get; set; }
public string[] results { get; set; }
}
执行后输出如下
[
{
"name": "Field1",
"results": [
"One",
"Two",
"Three"
]
},
{
"name": "Field2",
"results": [
"One",
"Two",
"Three",
"Four"
]
}
]
是否有任何实用程序或库可以将我的 JSON 转换为 DataTable。我检查了JSON.net example,但要求 JSON 必须采用以下格式才能将其转换为表格
string json = @"[{"Field1" : "One", "Field2": "One"}
{"Field1" : "Two", "Field2": "Two"}
{"Field1" : "Three", "Field2": "Three"}
{"Field1" : "", "Field2": "Four"}];
var table = JsonConvert.DeserializeObject<DataTable>(json);
另外,我的 JSON 是否可以转换为 JsonConvert.DeserializeObject 所需的格式的任何线索
这不是重复的,标记为重复的问题讨论了最好的方法,而不是我在此处提供的复杂 JSON 格式示例。
预期输出
Field1, Field2
One, One
Two, Two
Three, Three
null,Four
【问题讨论】:
-
你如何创建这个 JSON?
-
通过从html文件中解析一些数据
-
你的DataTable应该有什么结构?从您的示例中,我得出结论您想要 2 列:Field1 和 Field2,对吗?
-
@AntonMaiorov 正确。但是可能字段比较多,所以需要foreach循环