简体   繁体   English

JSON.Net将XML转换为JSON

[英]JSON.Net Converting XML into JSON

I have following xml; 我有以下xml;

<?xml version="1.0" encoding="utf-8" ?>
<XslMapper>
  <type name="article" xsl="http://localhost:8080/Xsl-a.xslt">
    <category name="1234" xsl="http://localhost:8080/Xsl-b.xslt"></category>
    <category name="1234" xsl="http://localhost:8080/Xsl-b.xslt"></category>
  </type>
  <type name="slideshow" xsl="http://localhost:8080/Xsl-c.xslt" >
    <category name="1234" xsl="http://localhost:8080/Xsl-b.xslt"></category>
  </type>
</XslMapper>

C# code for parsing; 用于解析的C#代码;

WebClient client = new WebClient();
            StringBuilder builder = new StringBuilder();
            string downloadString = client.DownloadString(XslMapperFileAddress);
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(downloadString);
            XmlWriter writer = XmlWriter.Create(builder, new XmlWriterSettings() { OmitXmlDeclaration = true });
            xml.Save(writer);
            string xmlString = builder.ToString();
            xml.LoadXml(xmlString);
            string jsonText = JsonConvert.SerializeXmlNode(xml, Formatting.Indented, true);
            jsonText = Regex.Replace(jsonText, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase);
            XslMapper xslMapper = JsonConvert.DeserializeObject<XslMapper>(jsonText);
            return xslMapper.XmlMapperTypes;

When I serialize this xml into json with json.net I am getting following result; 当我使用json.net将这个xml序列化为json时,我得到以下结果;

{
  "type": [
    {
      "name": "article",
      "xsl": "http://localhost:8080/Services/Xsl-a.xslt",
      "category": [
        {
          "name": "1234",
          "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
        },
        {
          "name": "1234",
          "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
        }
      ]
    },
    {
      "name": "slideshow",
      "xsl": "http://localhost:8080/Services/Xsl-c.xslt",
      "category": {
        "name": "1234",
        "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
      }
    }
  ]
}

as you can see first category section is parsed as an array (which I am intended to do) and second part converted as object. 如您所见,第一类别部分被解析为数组(我打算这样做),第二部分被转换为对象。 That's why I am getting error from JSON.NET 这就是为什么我从JSON.NET中得到错误

How can I parse second part as array like; 我如何将第二部分解析为数组?

"category": [
        {
          "name": "1234",
          "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
        }        
      ]
    },

Converting between JSON and XML contains example named Attribute to Force a JSON Array which says that you have to define a JSON namespace 在JSON和XML之间转换时,包含一个名为Attribute的示例,以强制执行JSON数组 ,其中要求您定义JSON名称空间

xmlns:json='http://james.newtonking.com/projects/json'

in the XML's root element and add an attribute 在XML的根元素中并添加一个属性

json:Array='true'

to the element you wish to be converted into array ( <category/> in your case). 到要转换为数组的元素(在本例中为<category/> )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM