5

I am trying to convert a JSON string into a list of objects but am getting the error

Cannot deserialize the current JSON object into type List because the type requires a JSON array to deserialize correctly.

I retrieve the json and it looks like this

{
    "Code":0,
    "Message":"OK",
    "Data":
    {
        "Houses":
        [
            {
                "Id":1,
                "Name":"House 1",
                "Area":"22.00",
                "ShortName":"H1",
                "FarmName":"Farm 1"
            },
            {
                "Id":2,
                "Name":"Farmi1 - House 1",
                "Area":"1000.00",
                "ShortName":"H1",
                "FarmName":"Farm 1"
            }
        ]
    }
}

I then say

List<House> Houses = JsonConvert.DeserializeObject<List<House>>(json); 
0

6 Answers 6

9

The JSON string you get is not a list, but an object which has a list on a nested level. You will have to deserialize the outermost structure and then get the respective nested property.

You can either define a whole class representing the complete structure of your data, or, if you are only interested in the List of Houses, just use JObjects

var o = JsonConvert.DeserializeObject<JObject>(json);
var h = o.Value<JObject>("Data")
    .Value<JArray>("Houses")
    .ToObject<List<Houses>>();
Sign up to request clarification or add additional context in comments.

Comments

2

You can't directly go at a sub-node in the json string. You'll have to create a class that has a property of 'houses' that is a list of houses and then deserialize that.

public class HouseList{
    public class HouseData{
       List<House> Houses {get;set;|
    }
    public string Code {get;set;}
    public string Message {get;set;}
   public HouseData Data {get;set;}
}

var houseList = JsonConvert.DeserializeObject<HouseList>(json).Data.Houses; 

Something like that.

1 Comment

Data is not a List<House> but an object containing a property Houses which is a List<House>
0

I assume you have a collection Houses built like this :

public class Houses : List<House>
{

}

You might need a root object that contains the root structure of your JSON

public class Datas
{
    public Houses Houses;
}

using Newtonsoft.Json;

public class RootObject
{
    [JsonProperty("Code")]
    public int Code { get; set; }

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

    [JsonProperty("Data")]
    public Datas Data { get; set; }
}

Then, deserialize all into

var MyJSON = JsonConvert.DeserializeObject<RootObject>(json);

You can access the houses with MyJSON.Data.Houses

1 Comment

Data is not a List<House> but an object containing a property Houses which is a List<House>
0

You can deserialize as follows using two classes:

class Program
{
    static void Main(string[] args)
    {
        string json = "{\"Code\":0,\"Message\":\"OK\",\"Data\":{\"Houses\":[{\"Id\":1,\"Name\":\"House 1\",\"Area\":\"22.00\",\"ShortName\":\"H1\",\"FarmName\":\"Farm 1\"},{\"Id\":2,\"Name\":\"Farmi1 - House 1\",\"Area\":\"1000.00\",\"ShortName\":\"H1\",\"FarmName\":\"Farm 1\"}]}}";
        var obj = JsonConvert.DeserializeObject<MainClass>(json);
        ShowObject(obj);
        Console.Read();
    }

    static void ShowObject(MainClass obj)
    {
        Console.WriteLine("Code: " + obj.Code);
        Console.WriteLine("Message: " + obj.Message);
        Console.WriteLine("Data:\n " + obj.Data.Keys.ElementAt(0));
        foreach (var house in obj.Data.Values.ElementAt(0))
        {
            Console.WriteLine("  Id: " + house.Id);
            Console.WriteLine("  Name: " + house.Name);
            Console.WriteLine("  Area: " + house.Area);
            Console.WriteLine("  ShortName: " + house.ShortName);
            Console.WriteLine("  FarmName: " + house.FarmName);
            Console.WriteLine();
        }
    }
}

class MainClass
{
    public int Code { get; set; }
    public string Message { get; set; }

    public Dictionary<string, House[]> Data { get; set; }
}

class House
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Area { get; set; }
    public string ShortName { get; set; }
    public string FarmName { get; set; }
}

Output: Code: 0 Message: OK Data: Houses Id: 1 Name: House 1 Area: 22.00 ShortName: H1 FarmName: Farm 1 Id: 2 Name: Farmi1 - House 1 Area: 1000.00 ShortName: H1 FarmName: Farm 1

Comments

0

run this code in http://rextester.com/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace Rextester
{
    public class House
    {
        public int Id {set; get;}
        public string Name {set; get;}
        public float Area{set; get;}
        public string ShortName{set; get;}
        public string FarmName{set; get;}

    }
    public class MyJason
    {
        public int Code { set; get; }
        public string Message { set; get; }
        public MyData Data { set; get; }

    }

    public class MyData
    {
        public MyData()
        {
            Houses = new List<House>();
        }
        public List<House> Houses { set; get; }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            string jason= "{'Code':0,'Message':'OK','Data':{'Houses':[{'Id':1,'Name':'House 1','Area':'22.00','ShortName':'H1','FarmName':'Farm 1'},{'Id':2,'Name':'Farmi1 - House 1','Area':'1000.00','ShortName':'H1','FarmName':'Farm 1'}]}}";
            MyJason myJson = JsonConvert.DeserializeObject<MyJason>(jason);
            List<House> Houses = new List<House>();
            Houses = myJson.Data.Houses;
            Console.WriteLine(Houses.FirstOrDefault().Name);

        }
    }
}

Comments

-1

The list of Houses in nested inside the parent object that has Code, Message, and Data. Try deserializing the parent object first, then you can extract the Houses from the Data.

Comments

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.