将JSON数组转换为XML

7

我试图将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>

我的问题是我希望将所有“功能”列在一个共同的元素下,就像“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 包含错误.. 请修正它!!.. - Moumit
2
现在已经改正了,很抱歉,我不应该手写它。 - peter
2个回答

2
DeserializeXmlNode()没有一种定制其JSON到XML转换方式的方法。如果要使用该方法获得所需的结果,则需要在将JSON转换为XML之前操作JSON,或在之后操作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());

Fiddle: https://dotnetfiddle.net/fxxQnL


0

使用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文章以获取更多帮助。

免责声明:本库的作者为我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接