首页 新闻 会员 周边 捐助

像这种格式的json 如何反序列化

0
悬赏园豆:200 [已解决问题] 解决于 2013-07-20 13:40

{

11:{

aa:{对象属性}

},

31:{

dd:{对象属性}

}

}

问题补充:
代码实例
{
"1001": { "id": 1001, "name": "速度之靴", "description": "<unique>唯一被动—强化移动:</unique>+25移动速度 <i>(同名的唯一被动效果不叠加。)</i>", "price": 325, "icon": "/content/item/1001.png", "children": [], "parents": ["3006", "3047", "3020", "3158", "3111", "3117", "3009"], "requiredchampion": "", "restrictedmaps": [], "tags": ["movement", "boots"] }, "1004": { "id": 1004, "name": "仙女护符", "description": "<stats>+3法力回复/5秒</stats>", "price": 180, "icon": "/content/item/1004.png", "children": [], "parents": ["3037", "3096", "3028", "3070", "3073", "1080"], "requiredchampion": "", "restrictedmaps": [], "tags": ["mana_regen"] }, "1006": { "id": 1006, "name": "治疗宝珠", "description": "<stats>+5生命回复/5秒</stats>", "price": 180, "icon": "/content/item/1006.png", "children": [], "parents": ["3077", "3097", "3096", "3083", "3190", "1080"], "requiredchampion": "", "restrictedmaps": [], "tags": ["health_regen"] }, "1011": { "id": 1011, "name": "巨人腰带", "description": "<stats>+380生命值</stats>", "price": 1000, "icon": "/content/item/1011.png", "children": [], "parents": ["3083", "3022", "3068", "3116", "3084"], "requiredchampion": "", "restrictedmaps": [], "tags": ["health"] }, "1018": { "id": 1018, "name": "灵巧披风", "description": "<stats>+15%暴击几率</stats>", "price": 730, "icon": "/content/item/1018.png", "children": [], "parents": ["3046", "3031", "3122"], "requiredchampion": "", "restrictedmaps": [], "tags": ["critical_strike"] }, "1026": { "id": 1026, "name": "爆裂魔杖", "description": "<stats>+40法术强度</stats>", "price": 860, "icon": "/content/item/1026.png", "children": [], "parents": ["3001", "3135", "3027", "3029", "3089", "3100", "3116", "3124"], "requiredchampion": "", "restrictedmaps": [], "tags": ["spell_damage"] }}


是不是要自己去写反序列化 不能用 JsonConvert.DeserializeObject来反序列化
王大湿的主页 王大湿 | 菜鸟二级 | 园豆:457
提问于:2013-07-20 11:54
< >
分享
最佳答案
-1

有对应的实体类吗?

收获园豆:100
dudu | 高人七级 |园豆:29570 | 2013-07-20 12:01

问题已补充

王大湿 | 园豆:457 (菜鸟二级) | 2013-07-20 12:41

@王大湿: 用Json.NET可以轻松搞定

dudu | 园豆:29570 (高人七级) | 2013-07-20 12:46

@dudu: 关键就是我不知道怎么搞定  JsonConvert.DeserializeObject<List<model.equip>>(json);的话就报错 有实例代码让我看看吗?或者文章

王大湿 | 园豆:457 (菜鸟二级) | 2013-07-20 12:48

@王大湿: 报什么错误?

dudu | 园豆:29570 (高人七级) | 2013-07-20 12:50

@dudu: 

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Lol_Phone_Web.model.equip]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '1001', line 2, position 9.

王大湿 | 园豆:457 (菜鸟二级) | 2013-07-20 12:51

@王大湿: 这个对应的类型不是List,是key/value,试试:

JsonConvert.DeserializeObject<Dictionary<string, model.equip>>(json);
dudu | 园豆:29570 (高人七级) | 2013-07-20 12:57

@dudu: 刚吃饭去了 可以 的 谢谢

王大湿 | 园豆:457 (菜鸟二级) | 2013-07-20 13:39
其他回答(1)
0

1. json 的格式有问题, 应该是这样的格式: {11:{aa:"cc"},31:{dd:"ee"}}

2. 如果json格式没问题,正常是通过与之对应的实体进行转换

3. 如果没有与之对应的实体,也可以使用 dynamic 进行转换

收获园豆:100
Yu | 园豆:12990 (专家六级) | 2013-07-20 12:43

我把问题补充了下 您可以看看  

支持(0) 反对(0) 王大湿 | 园豆:457 (菜鸟二级) | 2013-07-20 12:45

@王大湿: 

直接这样 dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);

不行吗

支持(0) 反对(0) Yu | 园豆:12990 (专家六级) | 2013-07-20 12:50

@Yu: 这样就可以了

var stuff = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Stuff>>(json);

 

public class Stuff {

public int id {get;set;}
public string name{get;set;}
public string description{get;set;}
public float price{get;set;}
public string icon{get;set;}
public string [] children {get;set;}
public string [] parents {get;set;}
public string requiredchampion{get;set;}
public string [] restrictedmaps {get;set;}
public string[] tags { get; set; }
}

支持(0) 反对(0) Yu | 园豆:12990 (专家六级) | 2013-07-20 13:00

@Yu: 嗯 谢谢

支持(0) 反对(0) 王大湿 | 园豆:457 (菜鸟二级) | 2013-07-20 13:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册