Details about the json:Array feature of Newtonsoft.JSON XML to JSON converter

一世执手 提交于 2019-12-12 03:43:25

问题


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

I have two questions:

  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?

  2. 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.

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;
    }

回答1:


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);

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);


来源:https://stackoverflow.com/questions/39479273/details-about-the-jsonarray-feature-of-newtonsoft-json-xml-to-json-converter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!