用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 asLevel
- You're trying to deserialize a
List<Employee>
, but your JSON only specifies a singleEmployee
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 aList<Home>
for theHome
property, and likewise for theOffice
property, but again the JSON just specifies an object value, not an array. Likewise for thelevel
property.
此外,对于Home
和Office
具有单独的类的事实非常令人讨厌,命名约定的混合也是如此.地址的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);