JSON.Net将XML转换成JSON [英] JSON.Net Converting XML into JSON

查看:227
本文介绍了JSON.Net将XML转换成JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML;

 <?XML版本=1.0编码=UTF-8> ; 
< XslMapper>
<类型名称=文章XSL =HTTP://本地主机:8080 / XSL-a.xslt>
<类别名称=1234XSL =HTTP://本地主机:8080 / XSL-b.xslt>< /类别>
<类别名称=1234XSL =HTTP://本地主机:8080 / XSL-b.xslt>< /类别>
< /类型>
<类型名称=幻灯片XSL =HTTP://本地主机:8080 / XSL-c.xslt>
<类别名称=1234XSL =HTTP://本地主机:8080 / XSL-b.xslt>< /类别>
< /类型>
< / XslMapper>解析



C#代码;

  Web客户端的客户端=新的WebClient(); 
StringBuilder的建设者=新的StringBuilder();
串downloadString = client.DownloadString(XslMapperFileAddress);
XmlDocument的XML =新的XmlDocument();
xml.LoadXml(downloadString);
XmlWriter的作家= XmlWriter.Create(建设者,新XmlWriterSettings(){OmitXmlDeclaration =真});
xml.Save(作家);
串的xmlString = builder.ToString();
xml.LoadXml(的xmlString);
串jsonText = JsonConvert.SerializeXmlNode(XML,Formatting.Indented,真正的);
jsonText = Regex.Replace(jsonText(小于= \?)(@)(* \?!:\\s),的String.Empty,RegexOptions.IgnoreCase) ;
XslMapper xslMapper = JsonConvert.DeserializeObject< XslMapper>(jsonText);
返回xslMapper.XmlMapperTypes;

当我序列化此XML为JSON与json.net我得到下面的结果;



  {
型:
{
名:文章,
XSL:HTTP://本地主机:8080 /服务/ XSL-a.xslt,
类:[
{
名:1234,
XSL:HTTP://本地主机:8080 /服务/ XSL-b.xslt
},
{
名:1234,
XSL:HTTP://本地主机:8080 /服务/ XSL-b.xslt
}
]
},
{
名 幻灯片,
XSL:HTTP://本地主机:8080 /服务/ XSL-c.xslt,
类:{
名:1234 ,
XSL:HTTP://本地主机:8080 /服务/ XSL-b.xslt
}
}
]
}

你可以看到第一类部分解析为一个数组(这我打算这样做)和第二部分转换作为对象。这就是为什么我从JSON.NET



如何解析第二部分作为阵列喜欢把自己的错误;

 类别:
{
名:1234,
XSL:HTTP://本地主机:8080 /服务/ XSL-b.xslt
}
]
},


解决方案

JSON和XML之间的转换包含例如名为属性强制JSON数组的它说,你必须定义一个JSON命名空间

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

在该XML的根元素,并添加一个属性。

  JSON:阵列=真

您希望(<转换成数组中的元素;类/>在您情况)。


I have following 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;

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;

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

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

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

in the XML's root element and add an attribute

json:Array='true'

to the element you wish to be converted into array (<category/> in your case).

这篇关于JSON.Net将XML转换成JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆