[英]Json.net deserialize to class
trying to deserialize this peace of json string using json.net, but i always get exception when deserializing. 试图使用json.net反序列化json字符串的这种和平性,但是反序列化时我总是会遇到异常。 Can anyone help me set up classes so that this will deserialize. 谁能帮助我设置课程,以便反序列化。
Thanks 谢谢
{
"type":"some_type",
"version":"0.1",
"data":
{
"item_name_1":
{
"id":266,
"key":"aaa",
"name":"aaa",
"title":"title_1",
"info":
{
"property_1":9,
"property_2":4,
}
},
"item_name_2":
{
"id":266,
"key":"bbb",
"name":"bbb",
"title":"title_2",
"info":
{
"property_1":93,
"property_2":24,
}
}
}
}
You have a comma(,) after the last property by info
, this shouldn't be there. 您在info
的最后一个属性之后有一个逗号(,),该属性不应存在。 I always use jsonlint to check if there are any errors in my json objects. 我总是使用jsonlint来检查json对象中是否有任何错误。
{
"type":"some_type",
"version":"0.1",
"data":
{
"item_name_1":
{
"id":266,
"key":"aaa",
"name":"aaa",
"title":"title_1",
"info":
{
"property_1":9,
"property_2":4
}
},
"item_name_2":
{
"id":266,
"key":"bbb",
"name":"bbb",
"title":"title_2",
"info":
{
"property_1":93,
"property_2":24
}
}
}
}
If we ignore the comma, which is apparently a typo 如果我们忽略逗号,这显然是错字
The problem is because item_name can be named different (item_name_1, _2... 问题是因为item_name可以命名为不同的名称(item_name_1,_2 ...
Yes, that is entirely correct; 是的,那是完全正确的; the names need to match. 名称需要匹配。 If you need to map something that doesn't match, you'll need to either provide a custom converter, or (usually simpler) map the data into a dictionary - ie you might have: 如果你需要一些映射不匹配,你要么需要提供自定义转换器,或(通常是简单的)将数据映射到一个字典-即你可能有:
public Dictionary<string, Item> data {get;set;}
(to represent the "data":
node) (代表"data":
节点)
This will result in key-value pairs, where each key is the field name ( "item_name_1"
, etc), and each value is the Item
instance (with properties id
, key
, name
, etc). 这将导致键值对,其中每个键是字段名称( "item_name_1"
等),每个值是Item
实例(具有属性id
, key
, name
等)。
ie 即
public class MyRootObject {
public string type {get;set;}
public string version {get;set;}
public Dictionary<string, Item> data {get;set;}
}
pulic class Item {
public int id {get;set;}
public string key {get;set;}
public string name {get;set;}
public string title {get;set;}
public Dictionary<string,int> info {get;set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.