gpt4 book ai didi

c# - JSON.NET:反序列化和合并 JSON 字符串

转载 作者:行者123 更新时间:2023-11-30 16:55:46 24 4
gpt4 key购买 nike

我有一个来自包含多个 JSON 对象的文件的 JSON 字符串,我需要将其反序列化为一个合并的 C# 对象。

File1.json

{
"manage_employees_section_title":
{
"value": "Manage employees",
"description": "The mange employees section title"
}
}
{
"manage_operations_section_title":
{
"value": "Manage operations",
"description": "The mange operations section title"
}
}

即使文件中有多个 JSON 对象,我真的很想从反序列化(或其他方式)中返回一个合并的 C# 对象,就像它来自这样的字符串:

{
"manage_employees_section_title":
{
"value": "Manage employees",
"description": "The mange employees section title"
},
"manage_operations_section_title":
{
"value": "Manage operations",
"description": "The mange operations section title"
}
}

JSON.NET 或任何其他工具是否可行?

非常感谢你们..

最佳答案

第一个代码块isn't valid JSON .如果您希望 JSON 库处理您的输入,您首先需要将其转换为有效的 JSON。

如果您的输入总是看起来像那样,您可以使用正则表达式找到 }\r\n\{ 并将其替换为逗号,然后将生成您的第二个例子:

var output = Regex.Replace(input, "\r\n}\r\n{", ",");

使用您提供的第一个示例的输入,现在生成第二个示例作为输出,它是有效的 JSON 并且可以适本地反序列化。

关于c# - JSON.NET:反序列化和合并 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29015056/

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