我现在正在与第三方系统接口。 哪些遍布世界各地,系统超过1000个。 我不知道格式会如何。 所以我想用设置动态解析它(而不是一个接一个地更改源代码(
现在我正在使用 C# Newtonsoft.Json 和 Json 文件包含分层数据。 想要将其转换为平面 Json 数据。
输入
{
"name": "root",
"children": [
{
"name": "First Top",
"children": [
{
"name": "First child",
"children": [
{
"name": "value1",
"size": "320"
}
]
},
{
"dep": "First Top",
"name": "First child",
"model": "value2",
"size": "320"
},
{
"dep": "First Top",
"name": "First child",
"model": "value3",
"size": "320"
},
{
"dep": "First Top",
"name": "First child",
"model": "value4",
"size": "320"
},
{
"dep": "First Top",
"name": "SECOND CHILD",
"model": "value1",
"size": "320"
},
{
"dep": "First Top",
"name": "SECOND CHILD",
"model": "value2",
"size": "320"
},
{
"dep": "First Top",
"name": "SECOND CHILD",
"model": "value3",
"size": "320"
},
{
"dep": "First Top",
"name": "SECOND CHILD",
"model": "value4",
"size": "320"
}
]
},
{
"name": "Second Top",
"children": [
{
"name": "First Child",
"children": [
{
"name": "value1",
"size": "320"
}
]
},
{
"dep": "Second Top",
"name": "First Child",
"model": "value2",
"size": "320"
},
{
"dep": "Second Top",
"name": "First Child",
"model": "value3",
"size": "320"
},
{
"dep": "Second Top",
"name": "First Child",
"model": "value4",
"size": "320"
},
{
"dep": "Second Top",
"name": "SECOND CHILD",
"model": "value1",
"size": "320"
},
{
"dep": "Second Top",
"name": "SECOND CHILD",
"model": "value2",
"size": "320"
},
{
"dep": "Second Top",
"name": "SECOND CHILD",
"model": "value3",
"size": "320"
},
{
"dep": "Second Top",
"name": "SECOND CHILD",
"model": "value4",
"size": "320"
}
]
},
{
"name": "Third Top",
"children": [
{
"name": "First Child",
"children": [
{
"name": "value2",
"size": "320"
}
]
},
{
"dep": "Third Top",
"name": "First Child",
"model": "value3",
"size": "320"
},
{
"dep": "Third Top",
"name": "First Child",
"model": "value4",
"size": "320"
},
{
"dep": "Third Top",
"name": "First Child",
"model": "value5",
"size": "320"
},
{
"dep": "Third Top",
"name": "Second Child",
"model": "value1",
"size": "320"
},
{
"dep": "Third Top",
"name": "Second Child",
"model": "value2",
"size": "320"
},
{
"dep": "Third Top",
"name": "Second Child",
"model": "value3",
"size": "320"
},
{
"dep": "Third Top",
"name": "Second Child",
"model": "value4",
"size": "320"
}
]
}
]
}
输出
{ "dep": "First Top", "name": "First child", "model": "value1", "size": "320" },
{ "dep": "First Top", "name": "First child", "model": "value2", "size": "320" },
{ "dep": "First Top", "name": "First child", "model": "value3", "size": "320" },
{ "dep": "First Top", "name": "First child", "model": "value4", "size": "320" },
{ "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
{ "dep": "First Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
{ "dep": "First Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
{ "dep": "First Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
{ "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" },
{ "dep": "Second Top", "name": "First Child", "model": "value2", "size": "320" },
{ "dep": "Second Top", "name": "First Child", "model": "value3", "size": "320" },
{ "dep": "Second Top", "name": "First Child", "model": "value4", "size": "320" },
{ "dep": "Second Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
{ "dep": "Second Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
{ "dep": "Second Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
{ "dep": "Second Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
{ "dep": "Third Top", "name": "First Child", "model": "value2", "size": "320" },
{ "dep": "Third Top", "name": "First Child", "model": "value3", "size": "320" },
{ "dep": "Third Top", "name": "First Child", "model": "value4", "size": "320" },
{ "dep": "Third Top", "name": "First Child", "model": "value5", "size": "320" },
{ "dep": "Third Top", "name": "Second Child", "model": "value1", "size": "320" },
{ "dep": "Third Top", "name": "Second Child", "model": "value2", "size": "320" },
{ "dep": "Third Top", "name": "Second Child", "model": "value3", "size": "320" },
{ "dep": "Third Top", "name": "Second Child", "model": "value4", "size": "320" }
我曾尝试动态循环,但无法成功。 我认为,需要通过递归调用方法来做到这一点。
提前谢谢。
Newtonsoft.Json库支持 LINQ,因此您可以使用 C# 的SelectMany()
(以及许多其他 LINQ 运算符(轻松平展集合。
例如:
using Newtonsoft.Json.Linq;
var input = "{ 'name': 'root', 'children': [ { 'name': 'First Top', 'children': [ { 'name': 'First child', 'children': [ { 'name': 'value1', 'size': '320' } ] }, { 'dep': 'First Top', 'name': 'First child', 'model': 'value2', 'size': '320' }, { 'dep': 'First Top', 'name': 'First child', 'model': 'value3', 'size': '320' }, { 'dep': 'First Top', 'name': 'First child', 'model': 'value4', 'size': '320' }, { 'dep': 'First Top', 'name': 'SECOND CHILD', 'model': 'value1', 'size': '320' }, { 'dep': 'First Top', 'name': 'SECOND CHILD', 'model': 'value2', 'size': '320' }, { 'dep': 'First Top', 'name': 'SECOND CHILD', 'model': 'value3', 'size': '320' }, { 'dep': 'First Top', 'name': 'SECOND CHILD', 'model': 'value4', 'size': '320' } ] }, { 'name': 'Second Top', 'children': [ { 'name': 'First Child', 'children': [ { 'name': 'value1', 'size': '320' } ] }, { 'dep': 'Second Top', 'name': 'First Child', 'model': 'value2', 'size': '320' }, { 'dep': 'Second Top', 'name': 'First Child', 'model': 'value3', 'size': '320' }, { 'dep': 'Second Top', 'name': 'First Child', 'model': 'value4', 'size': '320' }, { 'dep': 'Second Top', 'name': 'SECOND CHILD', 'model': 'value1', 'size': '320' }, { 'dep': 'Second Top', 'name': 'SECOND CHILD', 'model': 'value2', 'size': '320' }, { 'dep': 'Second Top', 'name': 'SECOND CHILD', 'model': 'value3', 'size': '320' }, { 'dep': 'Second Top', 'name': 'SECOND CHILD', 'model': 'value4', 'size': '320' } ] }, { 'name': 'Third Top', 'children': [ { 'name': 'First Child', 'children': [ { 'name': 'value2', 'size': '320' } ] }, { 'dep': 'Third Top', 'name': 'First Child', 'model': 'value3', 'size': '320' }, { 'dep': 'Third Top', 'name': 'First Child', 'model': 'value4', 'size': '320' }, { 'dep': 'Third Top', 'name': 'First Child', 'model': 'value5', 'size': '320' }, { 'dep': 'Third Top', 'name': 'Second Child', 'model': 'value1', 'size': '320' }, { 'dep': 'Third Top', 'name': 'Second Child', 'model': 'value2', 'size': '320' }, { 'dep': 'Third Top', 'name': 'Second Child', 'model': 'value3', 'size': '320' }, { 'dep': 'Third Top', 'name': 'Second Child', 'model': 'value4', 'size': '320' } ] } ] }";
var root = JObject.Parse(input);
var rootChildren = (JArray)root["children"];
var flattened = rootChildren.SelectMany(child => ((JArray)child["children"]).Skip(1)); // Skip the first one as it's irrelevant for the output.
// Create a JArray from the flattened collection (of JTokens)
// to be able to easily output it as a JSON string.
var jArray = new JArray(flattened);
var output = jArray.ToString();
你会注意到我跳过了children
第二个嵌套的第一个孩子。我这样做只是为了匹配您的预期输出,由于某种原因,该输出不包括每个嵌套集合的第一个子级。
作为使用此解决方案可以执行的操作的示例(给定动态嵌套级别的要求(,请考虑以下示例:https://dotnetfiddle.net/ikM1Ov
在此示例中,我修改了原始输入,将name
和size
字段包装在它们自己的对象中,并将它们置于新的children
嵌套级别(我还删除了第一个孩子的跳过(。