对于Json数据的应用,现在变得越来越简单了,只要有Json数据,引用最近的Json的DLL资源即可调用Json方法,DLL资源是一种封装好的一系列的函数,直接读取数据。
部分内容引用自:
http://blog.csdn.net/fengyun1989/article/details/7342029
Json格式数据:
{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2012年3月11日","date":"","week":"星期日","fchh":"11","cityid":"101010100","temp1":"6℃~-6℃","temp2":"9℃~-2℃","temp3":"13℃~0℃","temp4":"11℃~-1℃","temp5":"9℃~1℃"}}
添加Json引用:
Json最近Dll:http://json.codeplex.com/
下载后解压,找到Newtonsoft.Json.dll放到合适的文件夹,添加引用即可。
解析Json数据:
void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//用Jobject解析json数据
JObject json = JObject.Parse(e.Result);
weather = new WeatherInfo{
city = (string)json["weatherinfo"]["city"],
cityid = (string)json["weatherinfo"]["cityid"],
date_y = (string)json["weatherinfo"]["date_y"],
week = (string)json["weatherinfo"]["week"],
info = (string)json["weatherinfo"]["index_d"],
wind1 = (string)json["weatherinfo"]["wind1"],
temp1 = (string)json["weatherinfo"]["temp1"],
temp2 = (string)json["weatherinfo"]["temp2"],
temp3 = (string)json["weatherinfo"]["temp3"],
temp4 = (string)json["weatherinfo"]["temp4"],
weather1 = (string)json["weatherinfo"]["weather1"],
weather2 = (string)json["weatherinfo"]["weather2"],
weather3 = (string)json["weatherinfo"]["weather3"],
weather4 = (string)json["weatherinfo"]["weather4"]
};
}
解释如下:
wb_DownloadStringCompleted是Web Service的调用函数,e.Result表示返回结果,即上文中的Json格式数据的内容。