【问题标题】:Convert JSON array to XML将 JSON 数组转换为 XML
【发布时间】:2016-07-03 19:12:37
【问题描述】:

我正在尝试将 JSON 转换为 XML。我的 JSON 包含一系列汽车,每辆汽车都有一系列特征:

[
    {
        "car": {
            "features": [{
                "code": "1"
            }, {
                "code": "2"
            }]
        }
    },
    {
        "car": {
            "features": [{
                "code": "3"
            }, {
                "code": "2"
            }]
        }
    }
]

我正在将其转换为 XML:

// the tag name for each top level element in the json array
var wrappedDocument = string.Format("{{ car: {0} }}", jsonResult);
// set the root tag name
return JsonConvert.DeserializeXmlNode(wrappedDocument, "cars");

这是生成的 XML:

<cars>
   <car>
      <features>
        <code>1</code>
      </features>
      <features>
        <code>2</code>
      </features>
   </car>
   <car>
      <features>
        <code>3</code>
      </features>
      <features>
        <code>2</code>
      </features>
   </car>
</cars>

我的问题是我希望将所有“功能”列在一个公共元素下,就像“汽车”列在“汽车”下一样,以便 XML 看起来像这样:

<cars>
   <car>
       <features>
          <feature>
            <code>1</code>
          </feature>
          <feature>
            <code>2</code>
          </feature>
        </features>
   </car>
   <car>
       <features>
          <feature>
            <code>3</code>
          </feature>
          <feature>
            <code>2</code>
          </feature>
        </features>
   </car>
</cars>

这可能使用 Newtonsoft Json.NET 吗?感谢您的帮助!

【问题讨论】:

  • Json 包含错误 .. 更正它!!..
  • 现在已经更正了,对不起,我不应该手写的。

标签: c# arrays json xml json.net


【解决方案1】:

DeserializeXmlNode() 并没有办法自定义其 JSON 到 XML 转换的方式。要使用该方法获得所需的结果,您要么必须在将 JSON 转换为 XML 之前对其进行操作,要么在之后对 XML 进行操作。

在这种情况下,我认为使用 Json.Net 的 LINQ-to-JSON API 直接从 JSON 以您想要的形状构建 XML 可能更容易。你可以这样做:

var ja = JArray.Parse(jsonResult);
var xml = new XDocument(
    new XElement("cars", 
        ja.Select(c => 
            new XElement("car",
                new XElement("features", 
                    c["car"]["features"].Select(f => 
                        new XElement("feature", 
                            new XElement("code", (string)f["code"])
                        )
                    )
                )
            )
        )
    )
);

Console.WriteLine(xml.ToString());

小提琴:https://dotnetfiddle.net/fxxQnL

【讨论】:

    【解决方案2】:

    使用Cinchoo ETL - 一个开源库,您可以通过几行代码轻松地将 Xml 转换为 Json

    string json = @"
    [
        {
            ""car"": {
                ""features"": [{
                    ""code"": ""1""
                }, {
                    ""code"": ""2""
                }]
            }
        },
        {
            ""car"": {
                ""features"": [{
                    ""code"": ""3""
                }, {
                    ""code"": ""2""
                }]
            }
        }
    ]";
    StringBuilder sb = new StringBuilder();
    using (var p = ChoJSONReader.LoadText(json))
    {
        using (var w = new ChoXmlWriter(sb)
            .Configure(c => c.RootName = "cars")
            //.Configure(c => c.IgnoreRootName = true)
            .Configure(c => c.IgnoreNodeName = true)
            )
        {
            w.Write(p);
        }
    }
    Console.WriteLine(sb.ToString());
    

    输出:

    <cars>
      <car>
        <features>
          <feature>
            <code>1</code>
          </feature>
          <feature>
            <code>2</code>
          </feature>
        </features>
      </car>
      <car>
        <features>
          <feature>
            <code>3</code>
          </feature>
          <feature>
            <code>2</code>
          </feature>
        </features>
      </car>
    </cars>
    

    查看 CodeProject 文章以获得更多帮助。

    免责声明:我是这个库的作者。

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 2021-12-19
      • 2018-12-08
      • 2018-04-16
      • 2016-12-05
      • 2018-12-04
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多