问题描述
- 这个json 用C#应该怎么解析出来
-
"{"PAGE":[{"PCATE_PID":" ","PCATE_PNAME":"","PCATE_STATUS":"1","PCATE_DESC":"02","PCATE_NAME":"测试0","PCATE_ID":"3036703a459649a99fbda6af95fb2c88"},{"PCATE_PID":" ","PCATE_PNAME":"","PCATE_STATUS":"1","PCATE_DESC":"04","PCATE_NAME":"测试123","PCATE_ID":"342ec8e3ade6474caaf2187e141a2be5"},{"PCATE_PID":" ","PCATE_PNAME":"","PCATE_STATUS":"1","PCATE_DESC":"01","PCATE_NAME":"测试","PCATE_ID":"432419bfe66f4a978fba0982d081e616"},{"PCATE_PID":" ","PCATE_PNAME":"","PCATE_STATUS":"1","PCATE_DESC":"12","PCATE_NAME":"皮肤科诊室","PCATE_ID":"6b4bce703ff44b06a350b1c77db672c6"},{"PCATE_PID":" ","PCATE_PNAME":"","PCATE_STATUS":"1","PCATE_DESC":"3","PCATE_NAME":"普外科","PCATE_ID":"bd9bf58fc2154135903a58ba10778cb3"}]}";Dictionary<string, object> dic1 = JsonToDictionary(json.ToString());//将Json数据转成dictionary格式 foreach (KeyValuePair<string, object> item in dic1) { if (item.Key.ToString() == "PAGE")//获取header数据 { var subItem = dic1.Values.Count; //item.Value; 这里不知道应该怎样取值,请大神告知?? } } }
解决方案
你这个带有嵌套的,放入字典不是好办法
参考下面的代码
http://outofmemory.cn/code-snippet/1246/c-jiang-json-switch-dynamic-objectDynamicJsonConverter
http://www.cnblogs.com/tewuapple/archive/2012/09/14/2685271.html
解决方案二:
我就郁闷了,很简单的东西,为什么搜索不到正确的方法呢,还是说环境错误
netframework2.0下的JSON解析,我用的是Newtonsoft.Json,这是个开源库,在codeplex有下载
好像是用JsonReader类,但是我在2.0下怎么都提示问题
后来用JsonConvert.DeserializeObject解决了
jsonstr为要解析的JSON串
代码如下:
......
答案就在这里:关于C#中JSON解析
时间: 2024-09-27 19:48:06