【发布时间】:2019-04-12 14:06:40
【问题描述】:
我目前将Dictionary<string, JToken> item 定义为对象类。这不是根对象,而是第二级。
public class Root
{
public string name { get; set; }
public string description { get; set; }
public Table Table {get; set; }
}
public class Table
{
public Name name { get; set; }
[JsonExtensionData]
public Dictionary<string, JToken> item { get; set; }
}
有了这个,我希望在这个类中反序列化和排序传入的 json。目前所有的 json 对象都是无序的。我希望使用每个对象中的属性来创建订单。
请查看 JSON 结构:
{
'name': 'Customer Table',
'Table': {
'name': {
'id': 'name',
'type': 'info'
'description': 'Customer table info'
},
'RowId-123': {
'id': 'RowId-123',
'type': 'Row',
'children': [
'ColumnId-367'
],
'index': 1,
'parentId': 'custom'
},
'ColumnId-367': {
'id': 'ColumnId-367',
'type': 'Column',
'children': [],
'parentId': 'RowId-123'
},
'RowId-476': {
'id': 'RowId-476',
'type': 'Row'
'components': [
'ColumnId-317',
'ColumnId-327'
],
'index': 2,
'parentId': 'custom'
},
'ColumnId-317': {
'id': 'ColumnId-317',
'type': 'Column'
'components': [],
'index': 0,
'parentId': 'RowId-476'
},
'ColumnId-327': {
'id': 'ColumnId-327',
'type': 'Column',
'components': [],
'index': 1,
'parentId': 'RowId-476'
},
'TextContent12': {
'id': 'TextContent12',
'type': 'Text',
'index': 0,
'parentId': 'custom'
}
}
}
我在下面有 Linq 查询。
//Get all objects that has parent custom top level layer.
var top = root.Table.item.Values
.Where(x => x["parentId"].Value<string>() == "custom")
.OrderBy(i => i["index"]);
//Only looks at Index 0 and Row type.
var row = root.Table.item.Values
.FirstOrDefault(x => x["type"].Value<string>() == "Row" && x["index"].Value<int>() == 0);
//Get children column of row.
var column = root.Table.item.Values
.Where(x => x["parentId"]?.Value<string>() == row["id"].Value<string>())
.OrderBy(i => i["index"]);
我正在努力组合我的 linq 查询以循环遍历索引并获得我想要的输出。如何将查询组合在一起,这将根据不在行或行对象内的 Item 的最大值增加索引以产生以下输出。
Index 0 Content:
'TextContent12': {
'id': 'TextContent12',
'type': 'Text',
'index': 0,
'parentId': 'custom'
}
Index 1 Content:
'RowId-123': {
'id': 'RowId-123',
'type': 'Row',
'children': [
'ColumnId-367'
],
'index': 1,
'parentId': 'custom'
},
'ColumnId-367': {
'id': 'ColumnId-367',
'type': 'Column',
'children': [],
'parentId': 'RowId-123'
}
Index 2 Content:
'RowId-476': {
'id': 'RowId-476',
'type': 'Row'
'components': [
'ColumnId-317',
'ColumnId-327'
],
'index': 2,
'parentId': 'custom'
},
'ColumnId-317': {
'id': 'ColumnId-317',
'type': 'Column'
'components': [],
'index': 0,
'parentId': 'RowId-476'
},
'ColumnId-327': {
'id': 'ColumnId-327',
'type': 'Column',
'components': [],
'index': 1,
'parentId': 'RowId-476'
}
【问题讨论】: