天天看點

Windows Phone開發之Json資料解析

對于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格式資料的内容。