gpt4 book ai didi

c# - 使用 json.net 解析嵌套的 json

转载 作者:行者123 更新时间:2023-11-30 16:50:15 25 4
gpt4 key购买 nike

我的json反序列化有问题,下面是我的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,对象类就像

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

我尝试使用下面的代码反序列化它

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# 查询。

最佳答案

这里有几个问题:

  • 您提供的代码无法编译,因为您指定了一个名为 level 的类但将其用作 Level
  • 您正在尝试反序列化 List<Employee> ,但您的 JSON 仅指定一个 Employee目的;这与包含单个对象的对象数组不同
  • 您的 JSON 无效 - ObjectId("56bc28c436b252c406a67f17")根本不是 JSON 中的有效值。可能 Json.NET 对这种奇怪现象有一些支持,但如果你能使用有效的 JSON 会更好
  • 你的 Address类指定一个 List<Home>对于 Home属性(property),同样适用于 Office属性,但同样,JSON 仅指定一个对象值,而不是数组。同样对于 level属性(property)。

此外,您为 Home 设置了单独的类和 Office非常讨厌,命名约定的混合也是如此。地址的 JSON 结构远非理想,但我想你无法解决这个问题。

我真的无法修复 ObjectId问题,但我会将类构造为:

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:

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

关于c# - 使用 json.net 解析嵌套的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35333214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com