0

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": "ead1@yahoo.com",
                "Religion": ""
            },
            {
                "User-ID": "EAD002",
                "Country": "Senegal",
                "Continent": "Africa",
                "Gender": "Female",
                "Email": "ead2@yahoo.com",
                "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?

Haybee
  • 65
  • 1
  • 11
  • 1
    Does this answer your question? [Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List\`1](https://stackoverflow.com/questions/21358493/cannot-deserialize-the-current-json-object-e-g-namevalue-into-type-sy) – Pavel Anikhouski May 23 '20 at 16:17

2 Answers2

2

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;
}
Farhad Zamani
  • 5,381
  • 2
  • 16
  • 41
VollRahm
  • 397
  • 1
  • 16
  • This really help me out i will mark is as solved.. Thank you. I have a field of my JSON that returns the name as ***User-Code*** with an hyphen when i create my JSON model with same ***User-Code*** in c# it rejects the naming convention and when i use ***User_Code*** the Model does not return anything. How can i solve this? – Haybee May 23 '20 at 16:46
  • @Haybee see this [link](https://stackoverflow.com/a/13869651/7086678) – Farhad Zamani May 23 '20 at 16:49
  • @FarhadZamani This is at the point of creating my c# JSON Model. when i do ***public string User-Code { get; set; }*** c# rejects the User-Code naming unless i use a User_Code with and underscore. Can you show an example? – Haybee May 23 '20 at 17:00
  • You need to set a JsonProperty. Just type this above your field: [JsonProperty("User-Code")] @Haybee – VollRahm May 23 '20 at 17:03
  • Thank you.. This has been solved i had to use a Json Property to overide. – Haybee May 23 '20 at 17:04
0

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);