转自:http://www.cnblogs.com/shizhenglg/archive/2013/02/25/2932279.html
添加System.Servicemodel.Web程序集,using System.Runtime.Serialization.Json需要此程序集
public class TopRotateImage
{
private string _id;
public string id
{
get { return _id; }
set { _id = value; }
}
private string _image;
public string image
{
get { return _image; }
set { _image = value; }
}
private string _intro;
public string intro
{
get { return _intro; }
set { _intro = value; }
}
}
public class WeatherInfo
{
public string city { get; set; }
//public string cityid { get; set; }
//public string date_y { get; set; }
//public string week { get; set; }
//public string temp1 { get; set; }
//public string temp2 { get; set; }
//public string temp3 { get; set; }
//public string temp4 { get; set; }
//public string temp5 { get; set; }
//public string weather1 { get; set; }
//public string weather2 { get; set; }
//public string weather3 { get; set; }
//public string weather4 { get; set; }
//public string weather5 { get; set; }
//public string wind1 { get; set; }
//public string index48_d { get; set; } //index48_d
}
public class WeInfo
{
public WeatherInfo weatherinfo { get; set; }
}
以上是两个不同json需要的model
xaml
<ListBox x:Name="wList" SelectionChanged="wList_SelectionChanged_1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding intro}" Width="200" Height="200" Foreground="Red"/>
<!--<TextBlock Text="{Binding city}" Width="200" Height="200" Foreground="Red"/>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后台数据解析用到的帮助类
public class JsonParser
{
//序列化,json字符串
public static string Serialize(object objectToSerialize)
{
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
serializer.WriteObject(ms, objectToSerialize);
ms.Position = 0;
using (StreamReader reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
}
}
//反序列化,解析json转成对象
public static T Deserialize<T>(string jsonString)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(ms);
}
}
读取是需要这个类
public class ReadJson
{
/// <获取Json内容>
/// 获取Json内容
/// </获取Json内容>
/// <param name="jsonstream">Json流</param>
/// <returns>返回string 类型Json内容</returns>
public static string ReturnJsonContent(Stream jsonstream)
{
StreamReader sr = new StreamReader(jsonstream);
return sr.ReadToEnd();
}
}
后台的解析
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri("http://gaobo.zcom.com:8089/zcomreader/recommend/index_slide"));
//wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
//wc.OpenReadAsync(new Uri("http://gaobo.zcom.com:8089/zcomreader/recommend/index_slide"));
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
List<TopRotateImage> weInfo = new List<TopRotateImage>();
weInfo = JsonParser.Deserialize<List<TopRotateImage>>(e.Result);
wList.ItemsSource = weInfo;
}
//List<TopRotateImage> weInfo = new List<TopRotateImage>();
//void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
//{
// string str = ReadJson.ReturnJsonContent(e.Result);
// MessageBox.Show(str);
// weInfo = JsonParser.Deserialize<List<TopRotateImage>>(str);
// wList.ItemsSource = weInfo;
//}
//private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
//{
// WebClient wc = new WebClient();
// wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
// wc.OpenReadAsync(new Uri("http://m.weather.com.cn/data/101180101.html"));
//}
//List<WeatherInfo> weInfo = new List<WeatherInfo>();
//void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
//{
// string str = ReadJson.ReturnJsonContent(e.Result);
// MessageBox.Show(str);
// WeInfo weinfo = JsonParser.Deserialize<WeInfo>(str);
// weInfo.Add(weinfo.weatherinfo);
// //MessageBox.Show(weinfo.weatherinfo.city);
// wList.ItemsSource = weInfo;
//}
private void wList_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
//if (wList.SelectedIndex != -1)
//{
// MessageBox.Show(((WeatherInfo)wList.SelectedItem).city);
//}
if (wList.SelectedIndex != -1)
{
MessageBox.Show(((TopRotateImage)wList.SelectedItem).intro);
}
}
用newtinsoft.json解析
List<TopRotateImage> wilist = new List<TopRotateImage>();
//void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
//{
//MessageBox.Show(e.Result);
//if (e.Result.Length <= 0 || e.Error != null || e.Cancelled)
//{
// MessageBox.Show("获取天气预报失败!");
// return;
//}
//JObject json = JObject.Parse(e.Result);//用Jobject解析json数据
//wilist.Add(new WeatherInfo
//{
// city = (string)json["weatherinfo"]["city"],
// cityid = (string)json["weatherinfo"]["cityid"],
// date_y = (string)json["weatherinfo"]["date_y"],
// week = (string)json["weatherinfo"]["week"],
// index48_d = (string)json["weatherinfo"]["index48_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"],
// temp5 = (string)json["weatherinfo"]["temp5"],
// weather1 = (string)json["weatherinfo"]["weather1"],
// weather2 = (string)json["weatherinfo"]["weather2"],
// weather3 = (string)json["weatherinfo"]["weather3"],
// weather4 = (string)json["weatherinfo"]["weather4"],
// weather5 = (string)json["weatherinfo"]["weather5"]
//});
//wList.ItemsSource = wilist;
//}