<WP7>解析json数据(初用json.Net)
2012-03-06 21:43
495 查看
要解析类似以下如此数据:
创建了那么两个类供解析后存放数据用。
一开始考虑用DataContractJsonSerializer类,但是使用时候报securityException错误。。研究半天,无果。好像是这个类不支持解析如此复杂的json。
后来发现有个叫json.NET这个库。JSON.NET官网
里面有个叫LINQ TO JSON。貌似支持解析如此数据。
首先添加下载下来的的Newtonsoft.Json.dll添加进来。
添加代码:
依然解析以让数据。
成功!
不过要把解析出来的对象一个一个赋值还真是麻烦。。。
{"weatherinfo":{"city":"长安","cityid":"101110102","temp":"7","WD":"西风","WS":"1级","SD":"50%","WSE":"1","time":"18:40","isRadar":"0","Radar":""}}
创建了那么两个类供解析后存放数据用。
class WhetherInfo { public string city { get; set; } public string temp { get; set; } public string cityid { get; set; } public string WD { get; set; } public string WS { get; set; } public string SD { get; set; } public string WSE { get; set; } public string time { get; set; } public string isRader { get; set; } public string Rader { get; set; } } class Info { public WhetherInfo weatherinfo { get; set; } }
一开始考虑用DataContractJsonSerializer类,但是使用时候报securityException错误。。研究半天,无果。好像是这个类不支持解析如此复杂的json。
后来发现有个叫json.NET这个库。JSON.NET官网
里面有个叫LINQ TO JSON。貌似支持解析如此数据。
首先添加下载下来的的Newtonsoft.Json.dll添加进来。
添加代码:
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
依然解析以让数据。
JObject json = JObject.Parse(e.Result); Info info = new Info { weatherinfo = new WhetherInfo {//
//由上面需要解析的数据可知,我们需要的数据在在weatherinfo这个节点的下一个节点
city = (string)json["weatherinfo"]["city"], cityid = (string)json["weatherinfo"]["cityid"], isRader = (string)json["weatherinfo"]["isRader"], Rader = (string)json["weatherinfo"]["Rader"], SD = (string)json["weatherinfo"]["SD"], temp = (string)json["weatherinfo"]["temp"], time = (string)json["weatherinfo"]["time"], WD = (string)json["weatherinfo"]["WD"], WS = (string)json["weatherinfo"]["WS"], WSE = (string)json["weatherinfo"]["WSE"] } }; MessageBox.Show(info.weatherinfo.city);
成功!
不过要把解析出来的对象一个一个赋值还真是麻烦。。。
相关文章推荐
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
- <WP7>(四)手把手教你写天气预报程序:本地数据库SQL CE,XML数据解析
- asp.net ajax解析json数据(运用List<Hashtable>装数据)
- json解析简单数据<一>
- <项目一>java数据转为JSONObject,再转为JSONArray
- 解析Json数据:使用Gson进行解析json数据<二>
- Java解析txt文件中json数据到List<entity>,并存入数据库
- 【Qt编程】基于Qt的词典开发系列<九>--JSON数据解析
- FreeMarker解析List<JSONObject>数据的疑惑
- net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx 关于JSON对象解析List<XXX>在调用时出错问题
- 解析Json数据:使用FastJson进行解析json数据<三>
- Json解析简单数据<二>
- <项目一>怎么将数据转换为json格式
- <项目一>请教一个关于获取post json数据的问题
- iOS json解析遇到@"";@"<null>";null 处理
- Delphi-IOCP学习笔记<八>=======服务端的数据发送和JSonStreamObject的编码器
- <String>XX</String>类型的XML解析
- asp.net的<% %>总结
- Unity的Json解析<一>--读取Json文件
- android基础---->JSON数据的解析 企