0

I'm using Newtonsoft to deserialize an array of JSON objects. I'm able to successfully deserialize the entire object with the exception of the fully nested @attributes objects.

Here is a snippet of an item I'm trying to parse:

"item" : [{
"title" : "Bloody.Birthday.1981.1080p.BluRay.X264-7SinS",
"guid" : "https:\/\/api.nzb.su\/details\/35e799ce66c3290db629b68e3bac20f9",
"link" : "https://",
"comments" : "https://url",
"pubDate" : "Wed, 25 Jun 2014 12:47:16 -0400",
"category" : "Movies > HD",
"description" : "Bloody.Birthday.1981.1080p.BluRay.X264-7SinS",
"enclosure" : {
    "@attributes" : {
        "url" : "https://url",
        "length" : "6777211483",
        "type" : "application\/x-nzb"
    }
},
"attr" : [{
        "@attributes" : {
            "name" : "category",
            "value" : "2000"
        }
    }, {
        "@attributes" : {
            "name" : "category",
            "value" : "2040"
        }
    }, {
        "@attributes" : {
            "name" : "size",
            "value" : "6777211483"
        }
    }
  ]
 }
]

Here are my classes to drill down and populate the items:

public class item
{
    public string title { get; set; }
    public string pubDate { get; set; }
    public IList<Attr> attr { get; set; }
}

public class Attr
{

    public Attributes attribs { get; set; }
}


public class Attributes
{
    public string name { get; set; }
    public string value { get; set; }

}

When do I a count on the item.attr, I get the proper # but if I try to actually get the name/value of the Attributes in the list, it gives me a null error. I've done a lot of research and have been unable to determine why it's creating the appropriate items in the List but not the values within the item.

Any thoughts?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

0

Since @attributes is not a valid property name in C#, you need to use a [JsonProperty] attribute to map it to the JSON, e.g.:

public class Attr
{
    [JsonProperty("@attributes")]
    public Attributes attribs { get; set; }
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300