parsing nested json with json.net

我只是一个虾纸丫 提交于 2019-12-23 21:22:10

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/35333214/parsing-nested-json-with-json-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!