JSON.Net Xml Serialization misunderstands arrays

From Json.NET documentation:
http://james.newtonking.com/projects/json/help/?topic=html/ConvertingJSONandXML.htm

You can force a node to be rendered as an Array by adding the attribute json:Array='true' to the XML node you are converting to JSON. Also, you need to declare the json prefix namespace at the XML header xmlns:json='http://james.newtonking.com/projects/json' or else you will get an XML error stating that the json prefix is not declared.

The next example is provided by the documentation:

xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
        <name>Alan</name>
        <url>http://www.google.com</url>
        <role json:Array='true'>Admin</role>
      </person>";

Generated output:

{
  "person": {
    "@id": "1",
    "name": "Alan",
    "url": "http://www.google.com",
    "role": [
      "Admin"
    ]
  }
}

Leave a Comment