现在的位置: 首页 > 综合 > 正文

新时尚Windows8开发(32):Json数据处理(A)

2012年08月20日 ⁄ 综合 ⁄ 共 2790字 ⁄ 字号 评论关闭

SON是啥?大家不陌生了吧,估计有人比我还懂,这玩意儿其实我只懂点皮毛,对,就是皮毛,皮和毛,皮包着毛,你看看JSON对象是不是这样?

外面套着一对大括号({})是皮,里面有很多毛毛,其实一个JSON对象就好像一个字典集合,有key,也有value,当然,也可以没有key。你看,一个标准的JSON对象大概是这样的。

{

      '键名' : 键值,

      '键名' :  键值,

      '键名' : 键值

}

 

如果是集合,如数组之类的,就多个对象放在 [ ...] 中,用逗号隔开,记得是英文的逗号。

 

 

案例1:从JSON字符串产生JSON对象

这个例子,我们来“调研”一下,如何从一个JSON结构的字符串生成一个JSON对象。

1、新建一个空白页面的“板砖”风格应用。

2、主页的布局就直接Ctrl + V XAML代码了,不然又有人说:“这和Win8有毛关系啊,这不是WPF中的XAML吗?”

  1. <Page  
  2.     x:Class="App3.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:App3"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.       
  10.     <Page.Resources>  
  11.         <Style x:Key="tbstyle" TargetType="TextBlock">  
  12.             <Setter Property="FontSize" Value="25"/>  
  13.         </Style>  
  14.     </Page.Resources>  
  15.   
  16.     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
  17.         <StackPanel Margin="20">  
  18.             <Button Content="从JSON字符串生成对象" Click="onClick"/>  
  19.             <Grid Margin="5,19,6,0">  
  20.                 <Grid.RowDefinitions>  
  21.                     <RowDefinition Height="auto"/>  
  22.                     <RowDefinition Height="auto"/>  
  23.                     <RowDefinition Height="auto"/>  
  24.                     <RowDefinition Height="auto"/>  
  25.                 </Grid.RowDefinitions>  
  26.                 <Grid.ColumnDefinitions>  
  27.                     <ColumnDefinition Width="auto"/>  
  28.                     <ColumnDefinition Width="auto"/>  
  29.                 </Grid.ColumnDefinitions>  
  30.                 <TextBlock Grid.Row="0" Grid.Column="0" Text="编号:" Style="{StaticResource tbstyle}"/>  
  31.                 <TextBlock x:Name="tbNO" Grid.Column="1" Grid.Row="0" Style="{StaticResource tbstyle}"/>  
  32.                 <TextBlock Grid.Column="0" Grid.Row="1" Text="姓名:" Style="{StaticResource tbstyle}"/>  
  33.                 <TextBlock x:Name="tbName" Grid.Row="1" Grid.Column="1" Style="{StaticResource tbstyle}"/>  
  34.                 <TextBlock Grid.Column="0" Grid.Row="2" Text="城市:" Style="{StaticResource tbstyle}"/>  
  35.                 <TextBlock x:Name="tbCity" Grid.Row="2" Grid.Column="1" Style="{StaticResource tbstyle}"/>  
  36.                 <TextBlock Grid.Row="3" Grid.Column="0" Text="年龄:" Style="{StaticResource tbstyle}"/>  
  37.                 <TextBlock x:Name="tbAge" Grid.Row="3" Grid.Column="1" Style="{StaticResource tbstyle}"/>  
  38.             </Grid>  
  39.         </StackPanel>  
  40.     </Grid>  
  41. </Page>  


 

3、后台的代码,主要是处理按钮的Click事件,我贴个完整的。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using Windows.Foundation;  
  6. using Windows.Foundation.Collections;  
  7. using Windows.UI.Xaml;  

抱歉!评论已关闭.