用json.net解析嵌套的json [英] parsing nested json with json.net

查看:100
本文介绍了用json.net解析嵌套的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在json反序列化方面遇到问题,下面是我的json

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"
            }
        }
    ]
}

对于上述json,对象类类似于

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

但出现错误

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.

我该如何解决? 有什么办法,json结果来自mongodb c#查询.

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

推荐答案

这里有几个问题:

  • 您提供的代码将无法编译,因为您指定了一个名为level的类,但已将其用作Level
  • 您正在尝试反序列化List<Employee>,但是您的JSON仅指定了一个Employee对象.这与包含单个对象的对象数组不同
  • 您的JSON无效-ObjectId("56bc28c436b252c406a67f17")根本不是JSON中的有效值. Json.NET可能对此奇怪程度提供了一些支持,但是如果您可以使用有效的JSON,那就更好了
  • 您的Address类为Home属性指定了List<Home>,同样为Office属性指定了List<Home>,但是JSON再次仅指定了对象值,而不是数组.对于level属性也是如此.
  • 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.

此外,对于HomeOffice具有单独的类的事实非常令人讨厌,命名约定的混合也是如此.地址的JSON结构远非理想,但我想您无法解决.

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.

我无法真正解决ObjectId问题,但是我将类构造为:

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

除了ObjectId之外,这还将解析您使用以下方式提供的JSON:

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

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

这篇关于用json.net解析嵌套的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆