13

My JSON feed has nested objects like this:

{
"id": 1765116,
"name": "StrozeR",
"birth": "2009-08-12",
"avatar": "http:\/\/static.erepublik.com\/uploads\/avatars\/Citizens\/2009\/08\/12\/f19db99e9baddad73981d214a6e576ef_100x100.jpg",
"online": true,
"alive": true,
"ban": null,
"level": 61,
"experience": 183920,
"strength": 25779.42,
"rank": {
    "points": 133687587,
    "level": 63,
    "image": "http:\/\/www.erepublik.com\/images\/modules\/ranks\/god_of_war_1.png",
    "name": "God of War*"
},
"elite_citizen": false,
"national_rank": 6,
"residence": {
    "country": {
        "id": 81,
        "name": "Republic of China (Taiwan)",
        "code": "TW"
    },
    "region": {
        "id": 484,
        "name": "Hokkaido"
    }
}
}

and my object classes are like this:

class Citizen
{
    public class Rank
    {
        public int points { get; set; }
        public int level { get; set; }
        public string image { get; set; }
        public string name { get; set; }
    }
    public class RootObject
    {
        public int id { get; set; }
        public string name { get; set; }
        public string avatar { get; set; }
        public bool online { get; set; }
        public bool alive { get; set; }
        public string ban { get; set; }
        public string birth { get; set; }
        public int level { get; set; }
        public int experience { get; set; }
        public double strength { get; set; }
        public List<Rank> rank { get; set; }

    }
}

I try to parse my JSON data with following code

private async void getJSON()
{
    var http = new HttpClient();
    http.MaxResponseContentBufferSize = Int32.MaxValue;
    var response = await http.GetStringAsync(uri);

    var rootObject = JsonConvert.DeserializeObject<Citizen.RootObject>(response);
    uriTB.Text = rootObject.name;
    responseDebug.Text = response;
}

but I get the following error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Erepublik.Citizen+Rank]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

I can't even parse the value in the main object. Anyway to fix this? and how can I parse a value inside of a nested object? for example: "points" in "rank"

1
  • Just wondering how did you deserialize residence, country, region into C# classes. I am having the similar issue. Can you please post the code. Commented Jun 17, 2016 at 15:57

1 Answer 1

23

Like the error message says, your rank property in the .NET class is a List<Rank>, but in your JSON it's just a nested object, not an array. Change it to just a Rank instead of a List<Rank>.

Arrays in JSON (or any Javascript, really) are enclosed in []. The {} characters specify a single object. The CLR type has to roughly match the JSON type in order to deserialize. Object to object, array to array.

Sign up to request clarification or add additional context in comments.

3 Comments

Old post, but today it helped me a lot. time saving
So what is the solution? You need to create a wrapper for List<Rank>? Any cleaner solution?
No wrappers, if you want to use List<Rank> then the JSON needs to be in the form of a array of Ranks. Since you’re not using an array in the JSON, it’s just Rank , not List<Rank>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.