JSON.NET: Deserialise and merge JSON string

与世无争的帅哥 提交于 2020-01-15 02:55:26

问题


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"
    }
}

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"
    }
}

Is this possible with JSON.NET or any other tool ?

Many thanks in advance guys..


回答1:


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.

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{", ",");

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.




回答2:


If a combined XmlDocument is good enough, then you can:

        string json1 = "{ \"manage_employees_section_title\": {\"value\": \"Manage employees\",\"description\": \"The mange employees section title\"}}";
        string json2 = "{ \"manage_operations_section_title\": {\"value\": \"Manage operations\",\"description\": \"The mange operations section title\"}}";

        XmlDocument doc = new XmlDocument();
        var root = doc.CreateElement("element", "root", "");
        doc.AppendChild(root);

        var xmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json1);
        var xmlNode2 = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json2);

        foreach (XmlNode node in xmlNode.ChildNodes)
        {
            XmlNode imported = doc.ImportNode(node, true);
            doc.DocumentElement.AppendChild(imported);
        }

        foreach (XmlNode node in xmlNode2.ChildNodes)
        {
            XmlNode imported = doc.ImportNode(node, true);
            doc.DocumentElement.AppendChild(imported);
        }

which yields:

<?xml version="1.0" ?>
<root>
    <manage_employees_section_title>
        <value>Manage employees</value>
        <description>The mange employees section title</description>
    </manage_employees_section_title>
    <manage_operations_section_title>
        <value>Manage operations</value>
        <description>The mange operations section title</description>
    </manage_operations_section_title>
</root>


来源:https://stackoverflow.com/questions/29015056/json-net-deserialise-and-merge-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!