3

I might be silly, but I'm stuck on this one.

I'm coding in c# and attempting to parse a JSON string into an object. The problem is that the string contains a data-field which I call a pseudo-array. It isn't a real array, because it is missing the []'s....

I really don't want to create an object with 500 properties just to cater for what I assume to be a bug in the API's JSON response - but how on earth do I deserialize this into c# objects?

Below is the sample JSON from the API's documentation page. (I don't own the API - so I can't change the JSON)

Any advice will be appreciated. Thanks k

`

{
    "data": {
        "71489": { <---- This is supposed to be an item in an array - but it isn't 
            "air_by_date": 0, 
            "cache": {
                "banner": 1, 
                "poster": 1
            }, 
            "language": "en", 
            "network": "USA Network", 
            "next_ep_airdate": "", 
            "paused": 0, 
            "quality": "HD720p", 
            "show_name": "Law & Order: Criminal Intent", 
            "status": "Ended", 
            "tvdbid": 71489, 
            "tvrage_id": 4203, 
            "tvrage_name": "Law & Order: Criminal Intent"
        }, 
        "140141": {
            "air_by_date": 0, 
            "cache": {
                "banner": 0, 
                "poster": 0
            }, 
            "language": "fr", 
            "network": "CBS", 
            "next_ep_airdate": "2012-01-15", 
            "paused": 0, 
            "quality": "Any", 
            "show_name": "Undercover Boss (US)", 
            "status": "Continuing", 
            "tvdbid": 140141, 
            "tvrage_id": 22657, 
            "tvrage_name": "Undercover Boss"
        }, 
...
        "194751": {
            "air_by_date": 1, 
            "cache": {
                "banner": 1, 
                "poster": 1
            }, 
            "language": "en", 
            "network": "TBS Superstation", 
            "next_ep_airdate": 2011-11-28", 
            "paused": 0, 
            "quality": "Custom", 
            "show_name": "Conan (2010)", 
            "status": "Continuing", 
            "tvdbid": 194751, 
            "tvrage_id": 0, 
            "tvrage_name": ""
        }, 
        "248261": {
            "air_by_date": 0, 
            "cache": {
                "banner": 1, 
                "poster": 1
            }, 
            "language": "en", 
            "network": "Cartoon Network", 
            "next_ep_airdate": "", 
            "paused": 1, 
            "quality": "HD", 
            "show_name": "NTSF:SD:SUV::", 
            "status": "Continuing", 
            "tvdbid": 248261, 
            "tvrage_id": 28439, 
            "tvrage_name": "NTSF:SD:SUV"
        }
    }, 
    "message": "", 
    "result": "success"
}
`
Keil
  • 63
  • 4
  • In addition to the answer below, you could deserialize `data` to a `Dictionary`. See [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/q/24536533/3744182) and [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/q/34213566/3744182). – dbc Mar 29 '17 at 19:48

1 Answers1

0

The data property of your JSON object is an associative array. You need to access each element by the appropriate key (in this case a numeric string)

// convert from a String into a JObject
var data = JObject.Parse(json);

// access single property
Response.Write(data["data"]["71489"]);
Response.Write(data["data"]["140141"]);

// iterate all properties
foreach (JProperty prop in data["data"])
{
    Response.Write(prop.Name + ": " + prop.Value);

    // Also possible to access things like:
    // - prop.Value["air_by_date"]
    // - prop.Value["cache"]["banner"]
    // - prop.Value["cache"]["poster"]
    // - prop.Value["language"]
}
dana
  • 17,267
  • 6
  • 64
  • 88