有关Newtonsoft.JSON XML到JSON转换器的json:Array功能的详细信息 [英] Details about the json:Array feature of Newtonsoft.JSON XML to JSON converter

查看:305
本文介绍了有关Newtonsoft.JSON XML到JSON转换器的json:Array功能的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考以下使用"json:Array"的示例:在JSON和XML之间转换

Referencing this example of using "json:Array": Converting between JSON and XML

我有两个问题:

  1. 名称空间是否必须为"json"? IE.如果ns2匹配回 "xmlns:ns2='http://james.newtonking.com/projects/json'"可以吗?

  1. Does the namespace have to be "json"? I.e. if ns2 matched back to "xmlns:ns2='http://james.newtonking.com/projects/json'" would that work?

可以省略名称空间吗?我可以只输入"Array ='true'"吗?

Can the namespace be omitted? Can I just put "Array='true'"?

我将尝试通过反复试验进行测试,但认为可能有人会知道答案,或者将来有人会想知道.

I'm about to try to test by trial and error, but thought maybe somebody would know the answer, or someone in the future would like to know.

并不是很重要,但是我的XML是由BizTalk 2010生成的,并且我正在使用WCF CustomBehavior如下调用NewtonSoft:

Not that it matters a whole lot, but my XML is being generated by BizTalk 2010 and I'm using a WCF CustomBehavior to call NewtonSoft as follows:

private static ConvertedJSON ConvertXMLToJSON(string xml)
    {
    // To convert an XML node contained in string xml into a JSON string   
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    ConvertedJSON convertedJSON = new ConvertedJSON();
    convertedJSON.JSONtext = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None);
    convertedJSON.rootElement = doc.DocumentElement.Name;
    return convertedJSON;
    }

推荐答案

名称空间必须与它们提供的内容完全相同:

Looks like the namespace has to be exactly what they provide:

  string xmlToConvert2 = "<myRoot xmlns:json='http://james.newtonking.com/projects/json'><MyText json:Array='true'>This is the text here</MyText><Prices><SalesPrice>10.00</SalesPrice></Prices></myRoot>";
  string strJSON2 = ConvertXMLToJSON(xmlToConvert2);

与普通xml一样,名称空间前缀可以是任何值.后续工作与上述工作同样出色.

As with normal xml, the namespace prefix can be any value. The follow worked equally as well as the above.

string xmlToConvert3 = "<myRoot xmlns:abc='http://james.newtonking.com/projects/json'><MyText abc:Array='true'>This is the text here</MyText><Prices><SalesPrice>10.00</SalesPrice></Prices></myRoot>";
string strJSON3 = ConvertXMLToJSON(xmlToConvert3);

这篇关于有关Newtonsoft.JSON XML到JSON转换器的json:Array功能的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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