Haybee
Haybee

Reputation: 95

Issue in Deserializing JSON Response Data to List

i having issues getting the elements in data into a list.

I want to be able to get User_ID, Country, Continent and other elements to a list after which i will do a bulk insert to the database.

The Error i get Error Message An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in mscorlib.dll but was not handled in user code

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Country_API.Response]' 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<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.

This is the JSon Data returned from the API

{
    "Status": "200 Ok",
    "Message": "Data retrieved",
    "Response": {
        "current_page": 1,
        "data": [
            {
                "User-ID": "EAD001",
                "Country": "Ghana",
                "Continent": "Africa",
                "Gender": "Male",
                "Email": "[email protected]",
                "Religion": ""
            },
            {
                "User-ID": "EAD002",
                "Country": "Senegal",
                "Continent": "Africa",
                "Gender": "Female",
                "Email": "[email protected]",
                "Religion": "Muslim"
            }
        ]
    }
}

I am trying to Deserilize but it throws the above error.. this is what i am trying

if (result.IsSuccessStatusCode)
{
    string toJsonString = await result.Content.ReadAsStringAsync();

    var deserialize = JsonConvert.DeserializeObject<List<Response>>(toJsonString);
}

Json Model

public class Data
{
    public string User-ID { get; set; }
    public string Country { get; set; }
    public string Continent { get; set; }
    public string Gender { get; set; }
    public string Email { get; set; }
    public string Religion { get; set; }

}
public class Response
{
    public int current_page { get; set; }
    public IList<Data> data { get; set; }

}
public class Application
{
    public string Status { get; set; }
    public string Message { get; set; }
    public Response Response { get; set; }
}

How to i achieve this please?

Upvotes: 0

Views: 1035

Answers (2)

Ashish Sojitra
Ashish Sojitra

Reputation: 25

Issue is here . You have used "List" instead of "Response". bcoz in JSON "Response" is object not a list

 var deserialize = JsonConvert.DeserializeObject<List<Response>>(toJsonString);

use like this.

 var deserialize = JsonConvert.DeserializeObject<Response>(toJsonString);

Upvotes: 0

VollRahm
VollRahm

Reputation: 437

You're trying to deserialize the List inside the object. You need to deserialize the entire object. Try this:

if (result.IsSuccessStatusCode)
{
    string toJsonString = await result.Content.ReadAsStringAsync();

    var deserialize = JsonConvert.DeserializeObject<Application>(toJsonString);
    IList<Data> dataList = deserialize.Response.data;
}

Upvotes: 2

Related Questions