JSON.NET:Deserialise和合并JSON字符串 [英] JSON.NET: Deserialise and merge JSON string

查看:193
本文介绍了JSON.NET:Deserialise和合并JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 }\r\n\{ and replace it with a comma, which will then produce your second example:

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

通过您所提供的第一例的输入,这个现在生产的第二个例子作为输出,它是有效的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.

这篇关于JSON.NET:Deserialise和合并JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆