Winphone开发之JSON解析

下面这个实战目标很简单,就是从网上获取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);
                }
            }
        }

    }
}

最后能保存每一本图书的信息并且将名字显示出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值