【发布时间】:2018-09-30 02:39:15
【问题描述】:
我正在尝试将 JSON 字符串解析为 C# 类。 但是我无法让它工作。
JSON 看起来像这样
{
reports:
{
report:
{
id:"1"
step:
{
id:"2"
step:
[
{
id:"3"
step:
[
{
id:"4"
},
{
id:"5"
}
]
},
{
id:"6"
step:
{
id:"7"
}
}
]
}
}
}
}
我做了几个类来定义结构。
private class report
{
public mongoReports reports;
}
private class mongoReports
{
public mongoReport report;
}
private class mongoReport
{
public string id;
public step step;
}
private class step
{
public string id = "";
}
private class step
{
public string id = "";
public step step;
}
private class step
{
public string id = "";
public list<step> step;
}
问题在于 step 可以有 3 个定义。 1 没有和 step 属性, 1 步的类型为“步” 1 其中 step 是“step”类型的对象列表。
我怎样才能制作一个适合所有这些结构的合适的“步骤”类?
【问题讨论】:
-
将 json 复制到剪贴板,然后在 VS 中:“Edit”、“Paste Special”、“Paste JSON As Classes”;工作完成
-
@MarcGravell 我猜这是动态的层次结构,所以从长远来看,一次性 JSON-to-C# 操作无济于事。