C# Cannot Deserialize Complex Json Object With Array

So I use HttpClient() to get Json object from web api. The object contains the list of all provinces in a country.

This code calls the json deserialization:

rj = JsonConvert.DeserializeObject<RajaOngkirCity>(response);

The response is like this:

{
    "rajaongkir": {
        "query": {
            "key": "xxxxxxxxxxxxxxxxxxxxx"
        },
        "status": {
            "code": 200,
            "description": "OK"
        },
        "results": [{
                "province_id": "1",
                "province": "Bali"
            },
            {
                "province_id": "2",
                "province": "Bangka Belitung"
            },
        .....]
    }
}

These are my classes:

public class RajaOngkirState
{
    [JsonProperty("rajaongkir")]
    public RajaOngkirStateResult RajaOngkir { get; set; }
}

public class RajaOngkirStateResult
{
    [JsonProperty("query")]
    public RajaOngkirStateQuery StateQuery { get; set; }

    [JsonProperty("status")]
    public RajaOngkirStateStatus Status { get; set; }

    [JsonProperty("results")]
    public RajaOngkirStateResults Results { get; set; }
}

public class RajaOngkirStateQuery
{
    [JsonProperty("key")]
    public string Key { get; set; }
}

public class RajaOngkirStateStatus
{
    [JsonProperty("code")]
    public int Code { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
}

public class RajaOngkirStateResults
{
    public List<RajaOngkirStateList> States { get; set; }
}

public class RajaOngkirStateList
{
    [JsonProperty("province_id")]
    public int ProvinceId { get; set; }
    [JsonProperty("province")]
    public string Province { get; set; }
}

It seems that rajaongkir.results which causing me the problem.

Here are things I have tried:

  1. I tried to change province_id type into string, but still error.
  2. I tried to change the List<RajaOngkirStateList> States into Dictionary<string, RajaOngkirStateList> but still error.

The error I am getting:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ECommerce.ShippingMethod.RajaOngkir.RajaOngkirStateResults' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'rajaongkir.results', line 1, position 118.

Jon Skeet
people
quotationmark

Currently your Results property claims to be an object containing an array - as if your JSON for it looked like this:

"results": {
    states: [{
              "province_id": "1",
              "province": "Bali"
            },
            {
              "province_id": "2",
              "province": "Bangka Belitung"
            }
    ]}

Instead, the results property in the JSON is the list.

So all you need to do is remove the RajaOngkirStateResults class entirely, and change your Results property to:

[JsonProperty("results")]
public List<RajaOngkirStateList> Results { get; set; }

I'd personally rename RajaOngkirStateList to just RajaOngkirState, given that it's not a list, but that's a separate matter.

people

See more on this question at Stackoverflow