gpt4 book ai didi

c# - C#有解析多层级联JSON的库吗?

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:45 25 4
gpt4 key购买 nike

是否有一个库(首选 C#)来解决我称之为多级级联 JSON 的问题?

这是我的意思的一个例子:(伪代码/C#)

var json1 = @"{
""firstName"": ""John"",
""lastName"": ""Smith""
}";

var json2 = @"{
""firstName"": ""Albert""
}";

var json3 = @"{
""phone"": ""12345""
}";

var cascadingJSON = JSON.Cascade(json1, json2, json3);

结果(与 CSS 行为相同)

{
"firstName"": "Albert", /*Overridden*/
"lastName"": "Smith", /*Inherited*/
"phone"": "12345" }"; /*Added*/
}

编辑 1 - 更复杂的示例

const string json1 =
@"{
""firstName"": ""John"",
""lastName"": ""Smith"",
""age"": 25,
""address"":
{
""streetAddress"": ""21 2nd Street"",
""city"": ""New York"",
""state"": ""NY"",
""postalCode"": ""10021""
},
""phoneNumber"":
[
{
""type"": ""home"",
""number"": ""212 555-1234""
},
{
""type"": ""fax"",
""number"": ""646 555-4567""
}
]
}";

const string json2 =
@"{
""firstName"": ""John2"",
""lastName"": ""robert"",
""age"": 25,
""address"":
{
""state"": ""FL"",
},
""phoneNumber"":
[
{
""type"": ""fax"",
""number"": ""222 222-2222""
},
{
""type"": ""iphone"",
""number"": ""111 111-1111""
}
]
}";

const string json3 =
@"{
""firstName"": ""John3"",
""father"": ""guy""
}";

const string expectedResult =
@"{
""firstName"": ""John3"",
""lastName"": ""robert"",
""age"": 25,
""father"": ""guy"",
""address"":
{
""streetAddress"": ""21 2nd Street"",
""city"": ""New York"",
""state"": ""FL"",
""postalCode"": ""10021""
},
""phoneNumber"":
[
{
""type"": ""home"",
""number"": ""212 555-1234""
},
{
""type"": ""fax"",
""number"": ""222 222-2222""
},
{
""type"": ""iphone"",
""number"": ""111 111-1111""
}
]
}";

编辑2

在对需求进行更多思考之后,我发现更复杂的示例永远无法按原样运行。例如,级联功能无法知道某个电话号码是否已修改或是否为新号码。为了让它发挥作用,每个子实体都应该有一个唯一的标识符。

最佳答案

使用出色的 JSON.NET library 非常容易.此方法将对象与字符串、数字或对象的属性结合起来。

public static string Cascade(params string[] jsonArray)
{
JObject result = new JObject();
foreach (string json in jsonArray)
{
JObject parsed = JObject.Parse(json);
foreach (var property in parsed)
result[property.Key] = property.Value;
}
return result.ToString();
}

结果,给定你的例子:

{
"firstName": "Albert",
"lastName": "Smith",
"phone": "12345"
}

根据您更新的问题进行编辑:

通过调整此解决方案以递归方式工作,您可以合并子对象。以下示例将匹配您的预期结果(数组除外)。您将能够轻松地扩展此解决方案,以类似于合并对象 (JObject) 的方式合并数组 (JArray)。

public static string Cascade(params string[] jsonArray)
{
JObject result = new JObject();
foreach (string json in jsonArray)
{
JObject parsed = JObject.Parse(json);
Merge(result, parsed);
}
return result.ToString();
}

private static void Merge(JObject receiver, JObject donor)
{
foreach (var property in donor)
{
JObject receiverValue = receiver[property.Key] as JObject;
JObject donorValue = property.Value as JObject;
if (receiverValue != null && donorValue != null)
Merge(receiverValue, donorValue);
else
receiver[property.Key] = property.Value;
}
}

关于c# - C#有解析多层级联JSON的库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12631338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com