SON是啥?大家不陌生了吧,估计有人比我还懂,这玩意儿其实我只懂点皮毛,对,就是皮毛,皮和毛,皮包着毛,你看看JSON对象是不是这样?
外面套着一对大括号({})是皮,里面有很多毛毛,其实一个JSON对象就好像一个字典集合,有key,也有value,当然,也可以没有key。你看,一个标准的JSON对象大概是这样的。
{
'键名' : 键值,
'键名' : 键值,
'键名' : 键值
}
如果是集合,如数组之类的,就多个对象放在 [ ...] 中,用逗号隔开,记得是英文的逗号。
案例1:从JSON字符串产生JSON对象
这个例子,我们来“调研”一下,如何从一个JSON结构的字符串生成一个JSON对象。
1、新建一个空白页面的“板砖”风格应用。
2、主页的布局就直接Ctrl + V XAML代码了,不然又有人说:“这和Win8有毛关系啊,这不是WPF中的XAML吗?”
- <Page
- x:Class="App3.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:App3"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <Page.Resources>
- <Style x:Key="tbstyle" TargetType="TextBlock">
- <Setter Property="FontSize" Value="25"/>
- </Style>
- </Page.Resources>
- <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
- <StackPanel Margin="20">
- <Button Content="从JSON字符串生成对象" Click="onClick"/>
- <Grid Margin="5,19,6,0">
- <Grid.RowDefinitions>
- <RowDefinition Height="auto"/>
- <RowDefinition Height="auto"/>
- <RowDefinition Height="auto"/>
- <RowDefinition Height="auto"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="auto"/>
- <ColumnDefinition Width="auto"/>
- </Grid.ColumnDefinitions>
- <TextBlock Grid.Row="0" Grid.Column="0" Text="编号:" Style="{StaticResource tbstyle}"/>
- <TextBlock x:Name="tbNO" Grid.Column="1" Grid.Row="0" Style="{StaticResource tbstyle}"/>
- <TextBlock Grid.Column="0" Grid.Row="1" Text="姓名:" Style="{StaticResource tbstyle}"/>
- <TextBlock x:Name="tbName" Grid.Row="1" Grid.Column="1" Style="{StaticResource tbstyle}"/>
- <TextBlock Grid.Column="0" Grid.Row="2" Text="城市:" Style="{StaticResource tbstyle}"/>
- <TextBlock x:Name="tbCity" Grid.Row="2" Grid.Column="1" Style="{StaticResource tbstyle}"/>
- <TextBlock Grid.Row="3" Grid.Column="0" Text="年龄:" Style="{StaticResource tbstyle}"/>
- <TextBlock x:Name="tbAge" Grid.Row="3" Grid.Column="1" Style="{StaticResource tbstyle}"/>
- </Grid>
- </StackPanel>
- </Grid>
- </Page>
3、后台的代码,主要是处理按钮的Click事件,我贴个完整的。
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI.Xaml;