JSON.NET:Deserialise和合并JSON字符串字符串、JSON、NET、Deserialise

由网友(心之所向便是光)分享简介:我有一个JSON字符串的使用,我需要deserialise成一个多个JSON对象的文件即将合并的C#对象。I have a JSON string coming from a file with multiple JSON objects that I need to deserialise into one mer...

我有一个JSON字符串的使用,我需要deserialise成一个多个JSON对象的文件即将合并的C#对象。

I have a JSON string coming from a file with multiple JSON objects that I need to deserialise into one merged C# object.

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对象,我真的很想有从deserialisation(或其他方式)退一合并C#对象就像它来自一个字符串,像这样的:

Even though there are multiple JSON objects in the file, I would really like to have back from deserialisation (or some other way) one merged C# object like it came from a string like this:

{
   "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 或任何其他工具?

在提前家伙非常感谢。

推荐答案

第一个code座是不是有效的JSON 。如果你想JSON库来处理你的输入您首先需要将其转换成有效的JSON。

The first code block isn't valid JSON. If you want JSON libraries to deal with your input you'll first need to convert it into valid JSON.

如果你输入总是会像她那样,你可以使用正则表达式来找到} r ñ {并用逗号替换,这将进而产生你的第二个例子:

If you're input is always going to look like that, you could use a regex to find the }rn{ and replace it with a comma, which will then produce your second example:

var output = Regex.Replace(input, "rn}rn{", ",");

通过您所提供的第一例的输入,这个现在生产的第二个例子作为输出,它是有效的JSON,并可以适当地反序列化。

With the input of the first example you provided, this now produces the second example as output, which is valid JSON and can be deserialized appropriately.

阅读全文

相关推荐

最新文章