Aleem
Aleem

Reputation: 93

How to parse multi-level JSON Array

I am trying to parse multi-level json array, but I am getting the following exception:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Tractor.Models.UserDetails' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Here is the json response:

{
  "status": 1,
  "message": null,
  "userdetails": [
    {
      "ID": 9,
      "Name": "aleem",
      "Company_Name": null,
      "Email": null,
      "Password": null,
      "Created_Date": null,
      "Email_Confirm": null,
      "Token": null,
      "Phone": null,
      "Website": null,
      "allcompanies": [
        {
          "Name": "self",
          "allprojects": [
            {
              "ID": 1,
              "Name": "test"
            }
          ]
        }
      ],
      "screens": 3
    }
  ]
}

Below are the classes I have added to get the JSON Response:

class LoginApiResponse
{
        [JsonProperty("status")]
        public int Status { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }

        [JsonProperty("userdetails")]
        public UserDetails UserDetails { get; set; }

    }

class UserDetails
{
    [JsonProperty("ID")]
    public int ID { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }

    [JsonProperty("Company_Name")]
    public string Company_Name { get; set; }

    [JsonProperty("Email")]
    public string Email { get; set; }

    [JsonProperty("Password")]
    public string Password { get; set; }

    [JsonProperty("Created_Date")]
    public string Created_Date { get; set; }

    [JsonProperty("Email_Confirm")]
    public string Email_Confirm { get; set; }

    [JsonProperty("Token")]
    public string Token { get; set; }

    [JsonProperty("Phone")]
    public string Phone { get; set; }

    [JsonProperty("Website")]
    public string Website { get; set; }

    [JsonProperty("allcompanies")]
    public List<Company> Companies { get; set; }

    [JsonProperty("screens")]
    public int Screens { get; set; }
}

class Company
    {
        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("allprojects")]
        public List<Project> Projects {get;set;}
    }

class Project
    {
        [JsonProperty("ID")]
        public int ID { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }
    }

I am using the below code to serialize this JSON:

var response = client.Execute(request); 
var final = JsonConvert.DeserializeObject<LoginApiResponse>(response.Content);

Upvotes: 0

Views: 2318

Answers (3)

Maximilian Ast
Maximilian Ast

Reputation: 3499

Your problem is in the LoginApiResponse class.

The Property UserDetails should be a List<UserDetails> instead of UserDetails. So you get:

class LoginApiResponse
{
    [JsonProperty("status")]
    public int Status { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("userdetails")]
    public List<UserDetails> UserDetails { get; set; }

}

This is because you expect an Array/List in the JSON for userdetails:

{
  "status": 1,
  "message": null,
  "userdetails": [
      { /* ... */ },
      { /* ... */ }
  ]
}

Alternatively (if you don't expect a list of userdetails) you can change the json (from a list to an object) too:

{
  "status": 1,
  "message": null,
  "userdetails": 
      { /* ... */ }
}

Upvotes: 1

Peter
Peter

Reputation: 76

The way I do it is deserialize a JSON object into a dynamic variable, then access the properties you need on that dynamic variable, you can use those to initialise a model if you'd like.

Upvotes: -1

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

The JSON requires UserDetails field to be an array (or list) of UserDetails objects:

class LoginApiResponse
{
    //.........
    [JsonProperty("userdetails")]
    public UserDetails[] UserDetails { get; set; }
    //                ^^
    // Alternatively, use List<UserDetails> instead of UserDetails[]
}

Upvotes: 2

Related Questions