【发布时间】:2019-04-11 14:58:30
【问题描述】:
我将 DeserializeObject 转换为 C# 对象,但是我有具有动态对象名称的对象,因此我将其结构如下:
public class RootObject
{
public string name { get; set; }
public TableLayout table{ get; set; }
}
public class TableLayout
{
public Attributes attributes { get; set; } //Static
public Info info { get; set; } //Static
[JsonExtensionData]
public Dictionary<string, JToken> item { get; set; }
}
所以基本上任何出现的动态对象都将被添加到字典中,并且使用 JsonExtensionData 将填充属性的其余部分,而无需创建对象类。这是我的json:
string json = @"
{
"name": "Table 100",
"table": {
"attributes ": {
"id": "attributes",
"type": "attributes"
},
"info": {
"id": "info",
"type": "info"
},
"item-id12": {
"id": "item-id12",
"type": "Row"
"index": 0
},
"item-id16": {
"id": "item-id16",
"type": "Column"
"parentid": "item-id12"
},
"item-id21": {
"id": "item-id21",
"type": "Column",
"parentid": "item-id12"
}
}
}";
如何使用 type ="row" 和索引值(索引 1 的增量以评估下一行)属性来获取使用我的 Dictionary 中列对象的 parentId 的所有列。
期望的输出:
"item-id12": {
"id": "item-id12",
"type": "Row"
"index": 0
},
"item-id16": {
"id": "item-id16",
"type": "Column"
"parentid": "item-id12"
},
"item-id21": {
"id": "item-id21",
"type": "Column",
"parentid": "item-id12"
}
【问题讨论】:
-
我确定我已经尝试过这种方式,但它不起作用。我可以通过 rootobject = JsonConvert.DeserializeObject
(json) 访问所有字典。 rootobject.table.item 但我想过滤它,因为有很多行/列关系,因为有很多行对象具有多列。