parsing nested json with json.net

I have problems with json deserialization , below is my json

{
    "_id" : ObjectId("56bc28c436b252c406a67f17"),
    "empname": "dhiraj",
    "empcode": "123a",
    "level": {
        "levelID": 3,
        "levelDescription": "manager",
        "levelCode": "mg"
    },
    "Address": [
        {
            "Home": {
                "streetname": "Home",
                "city": "bbb",
                "state": "aaa"
            }
        },
        {
            "Office": {
                "streetname": "ofc",
                "city": "ccc",
                "state": "ddd"
            }
        }
    ]
}

And for above json the object classes are like

public class Employee
{
    public ObjectId _id { get; private set; }
    public string empname { get; set; }
    public string empcode { get; set; }
    public List<Level> level { get; set; }
    public List<Address> Address { get; set; }
}

public class level
{
    public string levelID { get; set; }
    public string levelDescription { get; set; }
    public string levelCode { get; set; }
}
public class Address
{
    public List<Home> Home { get; set; }
    public List<office> Office { get; set; }
}
public class Home
{
    public string streetname { get; set; }
    public string city { get; set; }
    public string state { get; set; }
}
public class office
{
    public string streetname { get; set; }
    public string city { get; set; }
    public string state { get; set; }
}

i tried to deserialize it using below code

Employee empobj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Employee>>(jsonData);

but got an error as

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

How can i fix it? Is there any way, the json result is from mongodb c# query.

Jon Skeet
people
quotationmark

There are several problems here:

  • The code you've given won't compile, as you've specified a class called level but used it as Level
  • You're trying to deserialize a List<Employee>, but your JSON only specifies a single Employee object; that's not the same as an array of objects containing a single object
  • Your JSON is invalid - ObjectId("56bc28c436b252c406a67f17") simply isn't a valid value in JSON. It may be that Json.NET has some support for this oddity, but it would be better if you could use valid JSON
  • Your Address class specifies a List<Home> for the Home property, and likewise for the Office property, but again the JSON just specifies an object value, not an array. Likewise for the level property.

Additionally, the fact that you've got separate classes for Home and Office is pretty nasty, as is the mixture of naming conventions. The JSON structure of the addresses is far from ideal, but I guess you can't fix that.

I can't really fix the ObjectId problem, but I'd structure the classes as:

public class Employee
{
    [JsonProperty("_id")]
    public ObjectId Id { get; private set; }

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

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

    [JsonProperty("level")]
    public Level Level { get; set; }

    [JsonProperty("Address")]
    public List<Address> Addresses { get; set; }
}

public class Level
{
    [JsonProperty("levelID")]
    public string Id { get; set; }

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

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

// This structure is unfortunate, but imposed by the JSON
public class Address
{
    [JsonProperty("Home")]
    public StreetAddress Home { get; set; }

    [JsonProperty("Office")]
    public StreetAddress Office { get; set; }
}

public class StreetAddress
{
    [JsonProperty("streetname")]
    public string StreetName { get; set; }

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

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

Aside from ObjectId, that will parse the JSON you've given using:

var employee = JsonConvert.DeserializeObject<Employee>(json);

people

See more on this question at Stackoverflow