WP8.1开发笔记一:JSON数据处理

一:创建一个普通的WP8.1应用

二:在解决方案选中项目,右击 “添加NuGet应用”,添加 json.net

三:把抓包的JSON地址,处理一下

      JSON格式在线校对:http://www.bejson.com/go.html?u=http://www.bejson.com/jsonviewernew/

      JSON生成C#类:http://tools.wx6.org/json2csharp/

四:输入相关的代码并且和前台页面绑定在一块:

      后台页面的代码:

   

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 PhoneApp1.Resources;
using Newtonsoft.Json;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
            // 用于本地化 ApplicationBar 的示例代码
            //BuildLocalizedApplicationBar();
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadStringAsync(new Uri("http://wireless.tianya.cn/v/focusStand/rank?type=1&pageSize=100", UriKind.Absolute));
            webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var root = JsonConvert.DeserializeObject<Root>(e.Result);
            lls.ItemsSource = root.Data.List;
        }

        private void lls_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lls.SelectedItem != null)
            {
                var list = (lls.SelectedItem as List);
                MessageBox.Show(list.CategoryId);
            }
        } 
    }
}

 

前台页面的代码:

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:LongListSelector x:Name="lls" SelectionChanged="lls_SelectionChanged">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding CategoryName}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </Grid>

完成。

源码:http://pan.baidu.com/s/1qWM8FRQ

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。