3

I am having an issue with the JSON DeserializeObject Method.

When My XML document has only one Branch inside the Braches node, I get an error because my class below says that the Branches is a List. I am unsure how to proceed with this because there could be ONE <Branch> or multiple <Branch> inside the <Branches>.

Here's my Error Message:

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'PluginConsoleTestCode.Branch[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'Openers.Opener[2].Branches.Branch.@id', line 1, position 689.

My Code Snippet:

// XML to JSON then JSON to RootObject
var json = JsonConvert.SerializeXmlNode(xmlDoc);
var response = JsonConvert.DeserializeObject<RootObject>(json);

Here's my XML:

<Openers>
    <Opener Name="Name1" UserId="username">
        <Branches>
            <Branch id="103"></Branch>
            <Branch id="104"></Branch>
            <Branch id="105"></Branch>
            <Branch id="106"></Branch>
            <Branch id="107"></Branch>
            <Branch id="108"></Branch>
        </Branches>
    </Opener>
    <Opener Name="Name2" UserId="username">
        <Branches>
            <Branch id="109"></Branch>
            <Branch id="110"></Branch>
            <Branch id="111"></Branch>
            <Branch id="112"></Branch>
        </Branches>
    </Opener>
    <Opener Name="Name3" UserId="username">
        <Branches>
            <Branch id="113"></Branch>
        </Branches>
    </Opener>
</Openers>

XML Converted to JSON:

{
  "Openers": {
    "Opener": [
      {
        "@Name": "Name1",
        "@UserId": "username",
        "Branches": {
          "Branch": [
            { "@id": "103" },
            { "@id": "104" },
            { "@id": "105" },
            { "@id": "106" },
            { "@id": "107" },
            { "@id": "108" }
          ]
        }
      },
      {
        "@Name": "Name2",
        "@UserId": "username",
        "Branches": {
          "Branch": [
            { "@id": "109" },
            { "@id": "110" },
            { "@id": "111" },
            { "@id": "112" }
          ]
        }
      },
      {
        "@Name": "Name3",
        "@UserId": "username",
        "Branches": {
          "Branch": { "@id": "113" }
        }
      }
    ]
  }
}

Here is my Classes:

public class Branch
{
    [JsonProperty("@id")]
    public string Id { get; set; }
}

public class Branches
{
    [JsonProperty("Branch")]
    public List<Branch> Branch { get; set; }
}

public class Opener
{
    [JsonProperty("@Name")]
    public string Name { get; set; }
    [JsonProperty("@UserId")]
    public string UserId { get; set; }
    public Branches Branches { get; set; }
}

public class Openers
{
    public List<Opener> Opener { get; set; }
}

public class RootObject
{
    public Openers Openers { get; set; }
}
7
  • 1
    you have -UserId and -Name in the JSON. Apr 24, 2015 at 15:19
  • @DanielA.White and -id instead of id
    – Alex
    Apr 24, 2015 at 15:20
  • Actually, that was a mistake. The JSON response does come back with @ Id and @ UserId and @ Name. That's not the issue. Fixed in code.
    – Turp
    Apr 24, 2015 at 15:22
  • The problem is that the JSON is being created with a single Branch element with multiple Id elements inside it, rather than multiple Branch elements inside Branches, each with a single Id element. Apr 24, 2015 at 15:23
  • Do you have control over the XML that is created? Apr 24, 2015 at 15:27

2 Answers 2

0

Why do you have so complicated structure? Why don't you just have following

public class Branch
{
    [JsonProperty("@id")]
    public string Id { get; set; }
}

public class Opener
{
    [JsonProperty("@Name")]
    public string Name { get; set; }
    [JsonProperty("@UserId")]
    public string UserId { get; set; }
    [XmlArray("Branches")]
    [XmlArrayItem("branch", typeof(Branch))]
    public List<Branch> Branches { get; set; }
}


public class RootObject
{
    [XmlArray("Openers")]
    [XmlArrayItem("opener", typeof(Opener))]
    public List<Opener> Openers { get; set; }
}

Other Attributes should be automically working. If not, add all XMLAttribute and XMLElement

Edit: Why use json.net to deserialize xml, when there are system xmlserializers?

2
  • This doesn't work for me. I get an error message. Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List1[PluginConsoleTestCode.Opener]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. Path 'Openers.Opener', line 1, position 21.
    – Turp
    Apr 24, 2015 at 15:57
  • Sorry for being so long, but i've found some repo with some test task i did a month before your question and forgot. Here is all that you need. Xml -> objects -> json, using json.net and nested data lists (link to data is somewhere in the solution)
    – maiksaray
    May 5, 2015 at 13:01
0

You can use the attribute json:Array to force the converter to convert a node to an array.

http://www.newtonsoft.com/json/help/html/ConvertXmlToJsonForceArray.htm

In your example it would look like this

<Openers xmlns:json='http://james.newtonking.com/projects/json' json:Array='true'>
<Opener Name="Name1" UserId="username">
    <Branches json:Array='true'>
        <Branch id="103"></Branch>
        <Branch id="104"></Branch>
        <Branch id="105"></Branch>
        <Branch id="106"></Branch>
        <Branch id="107"></Branch>
        <Branch id="108"></Branch>
    </Branches>
</Opener>
<Opener Name="Name2" UserId="username">
    <Branches json:Array='true'>
        <Branch id="109"></Branch>
        <Branch id="110"></Branch>
        <Branch id="111"></Branch>
        <Branch id="112"></Branch>
    </Branches>
</Opener>
<Opener Name="Name3" UserId="username">
    <Branches json:Array='true'>
        <Branch id="113"></Branch>
    </Branches>
</Opener>
</Openers>
7
  • I'm getting an error on this...
    – Turp
    Apr 24, 2015 at 17:01
  • @Turp What is the error you are getting on this? Apr 24, 2015 at 17:02
  • Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[PluginConsoleTestCode.Opener]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
    – Turp
    Apr 24, 2015 at 17:03
  • To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
    – Turp
    Apr 24, 2015 at 17:03
  • Has xmlns:json="http://james.newtonking.com/projects/json" in the <Openers> tag. As well as json:Array="true" in each Branches tag
    – Turp
    Apr 24, 2015 at 17:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.