下面这个实战目标很简单,就是从网上获取JSON然后在ListBox上显示出来,基础知识前面几篇博客都讲到了,这里只是综合训练。
首先VS要解析JSON还是得要做一些工作的,详情看一下:
http://jingyan.baidu.com/article/6fb756ecd2b051241858fbef.html
首先看一下我们要解析的JSON:
[{"count": 17, "filename": "Lab1.pdf", "height": 744, "width": 1052, "file_id": "25"}, {"count": 13, "filename": "22.pdf", "height": 744, "width": 1052, "file_id": "26"}, {"count": 13, "filename": "22.pdf", "height": 744, "width": 1052, "file_id": "27"}, {"count": 8, "filename": "\u6613\u4f20-613.pdf", "height": 744, "width": 1052, "file_id": "31"}]
这里保存着一些电子书的信息,我们需要提取有用的信息ID和NAME,并且将NAME显示出来。
先创建类来保存每一项的信息:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JsonExample
{
/// <summary>
/// 存储每一项的消息
/// </summary>
public class BookInfo
{
public String BookId { get; set; }
public String BookName { get; set; }
public BookInfo(String i, String n)
{
BookId = i;
BookName = n;
}
public BookInfo()
{ }
}
}
然后弄一下界面XAML:
<phone:PhoneApplicationPage
x:Class="JsonExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid>
<ListBox x:Name="Lst">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=BookName}" Margin="70 10" FontSize="18"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</phone:PhoneApplicationPage>
这里面的布局上一篇BLOG讲了。
然后就是最关键的隐藏CS文件了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using JsonExample.Resources;
using System.Collections.ObjectModel;
using System.IO;
using Newtonsoft.Json.Linq;
namespace JsonExample
{
public partial class MainPage : PhoneApplicationPage
{
private ObservableCollection<BookInfo> books;
private String API = "http://sysucs.org:8080/list";
// 构造函数
public MainPage()
{
InitializeComponent();
books = new ObservableCollection<BookInfo>();
Lst.ItemsSource = books;
Download(API);
}
private void Download(String uri)
{
System.Net.WebClient client = new System.Net.WebClient();
client.OpenReadAsync(new Uri(uri));
client.OpenReadCompleted += new System.Net.OpenReadCompletedEventHandler(webclient_openReadCompleted);
}
private void webclient_openReadCompleted(object sender, System.Net.OpenReadCompletedEventArgs e)
{
using (StreamReader s = new StreamReader(e.Result))
{
string str = s.ReadToEnd();
JArray arr = new JArray(str);
var jsonVals = JArray.Parse(str);
foreach (JObject obj in jsonVals)
{
BookInfo info = new BookInfo();
info.BookId = (String)obj["file_id"];
info.BookName = (String)obj["filename"];
books.Add(info);
}
}
}
}
}
最后能保存每一本图书的信息并且将名字显示出来。