【问题标题】:JSON.NET: Deserialise and merge JSON stringJSON.NET:反序列化和合并 JSON 字符串
【发布时间】:2015-03-12 16:20:46
【问题描述】:

我有一个 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 或任何其他工具可以做到这一点吗?

非常感谢各位..

【问题讨论】:

  • @dav_i 我只是在想同样的事情,非常感谢。

标签: c# .net json json.net json-deserialization


【解决方案1】:

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

如果您的输入总是这样,您可以使用正则表达式查找 }\r\n\{ 并将其替换为逗号,这将生成您的第二个示例:

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

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

【讨论】:

    【解决方案2】:

    如果组合的 XmlDocument 足够好,那么您可以:

            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);
            }
    

    产生:

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多