新时尚Windows8开发(33):Json数据处理(B)
2013-01-16 10:15
288 查看
上一节我们讨论了如何手动方式来处理JSON对象数据,可能你也发现了,是有些痛苦,那么,用什么方法来读写JSON数据才算好玩?有的,如果你耍过WCF,或许已经想到了——JSON序列化和反序列化。
DataContractJsonSerializer类位于System.Runtime.Serialization.Json命名空间,我们只需要简单的调用两个方法就可以完成序列化和反序列化。
WriteObject:序列化,把对象写入JSON数据;
ReadObject:反序列化,从JSON数据中读入对象数据。
要完成今天的实例,我们先要定义一个类作为测试,这里我就举一个简单的,员工类,它有三个属性:姓名,联系电话,简介。定义如下。
[csharp] view
plaincopyprint?
public class Employee
{
/// <summary>
/// 员工姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 联系手机
/// </summary>
public string Phone { get; set; }
/// <summary>
/// 简介
/// </summary>
public string Description { get; set; }
}
对于UI,可以参考下面的XAML,我就不详细说,你会看得懂的。
[html] view
plaincopyprint?
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" Grid.Column="0" Margin="17">
<TextBlock Text="员工姓名:"/>
<TextBox x:Name="txtName" Margin="0,2,0,14"/>
<TextBlock Text="联系电话:"/>
<TextBox x:Name="txtPhone" Margin="0,2,0,15"/>
<TextBlock Text="员工简介:"/>
<TextBox x:Name="txtDesc" Margin="0,2,0,26"/>
<Button Content="保存数据" Click="onSave"/>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="1" Margin="15">
<Button Content="加载数据" Click="onLoadData"/>
<TextBlock x:Name="tbInfo" TextWrapping="Wrap" Margin="0,15,0,0"/>
</StackPanel>
</Grid>
</Page>
切换到代码视图,参考下面代码:
[csharp] view
plaincopyprint?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// 引入以下命名空间
using System.Runtime.Serialization.Json;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void onLoadData(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.CommitButtonText = "打开";
picker.FileTypeFilter.Add(".json");
picker.SuggestedStartLocation = PickerLocationId.Desktop;
StorageFile dataFile = await picker.PickSingleFileAsync();
if (dataFile != null)
{
// 开始读取文件
using (var stream = await dataFile.OpenSequentialReadAsync())
{
// 反序列化
DataContractJsonSerializer sz = new DataContractJsonSerializer(typeof(Employee));
Employee emp = (Employee)sz.ReadObject(stream.AsStreamForRead());
this.tbInfo.Text = string.Format("员工姓名:{0}\n联系电话:{1}\n简介:{2}", emp.Name, emp.Phone, emp.Description);
}
}
}
private async void onSave(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(this.txtName.Text) || string.IsNullOrWhiteSpace(this.txtPhone.Text) || string.IsNullOrWhiteSpace(this.txtDesc.Text))
return;
// 选择文件保存位置
FileSavePicker picker = new FileSavePicker();
picker.CommitButtonText = "保存";
picker.SuggestedStartLocation = PickerLocationId.Desktop;
picker.FileTypeChoices.Add("JSON数据文件", new string[] { ".json" });
StorageFile data = await picker.PickSaveFileAsync();
if (data != null)
{
using (var stream = await data.OpenAsync(FileAccessMode.ReadWrite))
{
// 创建新对象
Employee emp = new Employee { Name = this.txtName.Text, Phone = this.txtPhone.Text, Description = this.txtDesc.Text };
// 开始序列化
DataContractJsonSerializer srz = new DataContractJsonSerializer(emp.GetType());
srz.WriteObject(stream.AsStreamForWrite(), emp);
}
this.ShowMessageBox("保存成功。");
}
}
private async void ShowMessageBox(string msg)
{
MessageDialog dialog = new MessageDialog(msg);
await dialog.ShowAsync();
}
}
public class Employee
{
/// <summary>
/// 员工姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 联系手机
/// </summary>
public string Phone { get; set; }
/// <summary>
/// 简介
/// </summary>
public string Description { get; set; }
}
}
就是这么简单,按下F5运行。在页面左边输入相应内容,保存。
用记事本打开刚才保存的文件,你会看到以下内容:
回到应用程序,在页面右边,加载刚才保存的JSON,如下图所示:
到此为止,基本上完成了JSON序列化和反序列的工作了,但是,还有一个问题,我们继续讨论一下,刚才我们用记事本查看保存的JSON数据已经看到,JSON对象的字段名和类的属性名是一样的,那么,有些时候,我们并不希望这样。如:在类中姓名的属性名是Name,但在JSON数据中我希望它是emp_name。可以实现吗?答案是肯定的。
这时候,我们只需把Employee类的定义改一下就可以了。
[csharp] view
plaincopyprint?
[System.Runtime.Serialization.DataContract]
public class Employee
{
/// <summary>
/// 员工姓名
/// </summary>
[System.Runtime.Serialization.DataMember(Name = "emp_name")]
public string Name { get; set; }
/// <summary>
/// 联系手机
/// </summary>
[System.Runtime.Serialization.DataMember(Name = "emp_phoneno")]
public string Phone { get; set; }
/// <summary>
/// 简介
/// </summary>
[System.Runtime.Serialization.DataMember(Name = "emp_desc")]
public string Description { get; set; }
}
如此一来,所保存的JSON数据就如下图所示:
转载:http://blog.csdn.net/tcjiaan/article/details/8191186
DataContractJsonSerializer类位于System.Runtime.Serialization.Json命名空间,我们只需要简单的调用两个方法就可以完成序列化和反序列化。
WriteObject:序列化,把对象写入JSON数据;
ReadObject:反序列化,从JSON数据中读入对象数据。
要完成今天的实例,我们先要定义一个类作为测试,这里我就举一个简单的,员工类,它有三个属性:姓名,联系电话,简介。定义如下。
[csharp] view
plaincopyprint?
public class Employee
{
/// <summary>
/// 员工姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 联系手机
/// </summary>
public string Phone { get; set; }
/// <summary>
/// 简介
/// </summary>
public string Description { get; set; }
}
对于UI,可以参考下面的XAML,我就不详细说,你会看得懂的。
[html] view
plaincopyprint?
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" Grid.Column="0" Margin="17">
<TextBlock Text="员工姓名:"/>
<TextBox x:Name="txtName" Margin="0,2,0,14"/>
<TextBlock Text="联系电话:"/>
<TextBox x:Name="txtPhone" Margin="0,2,0,15"/>
<TextBlock Text="员工简介:"/>
<TextBox x:Name="txtDesc" Margin="0,2,0,26"/>
<Button Content="保存数据" Click="onSave"/>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="1" Margin="15">
<Button Content="加载数据" Click="onLoadData"/>
<TextBlock x:Name="tbInfo" TextWrapping="Wrap" Margin="0,15,0,0"/>
</StackPanel>
</Grid>
</Page>
切换到代码视图,参考下面代码:
[csharp] view
plaincopyprint?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// 引入以下命名空间
using System.Runtime.Serialization.Json;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void onLoadData(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.CommitButtonText = "打开";
picker.FileTypeFilter.Add(".json");
picker.SuggestedStartLocation = PickerLocationId.Desktop;
StorageFile dataFile = await picker.PickSingleFileAsync();
if (dataFile != null)
{
// 开始读取文件
using (var stream = await dataFile.OpenSequentialReadAsync())
{
// 反序列化
DataContractJsonSerializer sz = new DataContractJsonSerializer(typeof(Employee));
Employee emp = (Employee)sz.ReadObject(stream.AsStreamForRead());
this.tbInfo.Text = string.Format("员工姓名:{0}\n联系电话:{1}\n简介:{2}", emp.Name, emp.Phone, emp.Description);
}
}
}
private async void onSave(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(this.txtName.Text) || string.IsNullOrWhiteSpace(this.txtPhone.Text) || string.IsNullOrWhiteSpace(this.txtDesc.Text))
return;
// 选择文件保存位置
FileSavePicker picker = new FileSavePicker();
picker.CommitButtonText = "保存";
picker.SuggestedStartLocation = PickerLocationId.Desktop;
picker.FileTypeChoices.Add("JSON数据文件", new string[] { ".json" });
StorageFile data = await picker.PickSaveFileAsync();
if (data != null)
{
using (var stream = await data.OpenAsync(FileAccessMode.ReadWrite))
{
// 创建新对象
Employee emp = new Employee { Name = this.txtName.Text, Phone = this.txtPhone.Text, Description = this.txtDesc.Text };
// 开始序列化
DataContractJsonSerializer srz = new DataContractJsonSerializer(emp.GetType());
srz.WriteObject(stream.AsStreamForWrite(), emp);
}
this.ShowMessageBox("保存成功。");
}
}
private async void ShowMessageBox(string msg)
{
MessageDialog dialog = new MessageDialog(msg);
await dialog.ShowAsync();
}
}
public class Employee
{
/// <summary>
/// 员工姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 联系手机
/// </summary>
public string Phone { get; set; }
/// <summary>
/// 简介
/// </summary>
public string Description { get; set; }
}
}
就是这么简单,按下F5运行。在页面左边输入相应内容,保存。
用记事本打开刚才保存的文件,你会看到以下内容:
回到应用程序,在页面右边,加载刚才保存的JSON,如下图所示:
到此为止,基本上完成了JSON序列化和反序列的工作了,但是,还有一个问题,我们继续讨论一下,刚才我们用记事本查看保存的JSON数据已经看到,JSON对象的字段名和类的属性名是一样的,那么,有些时候,我们并不希望这样。如:在类中姓名的属性名是Name,但在JSON数据中我希望它是emp_name。可以实现吗?答案是肯定的。
这时候,我们只需把Employee类的定义改一下就可以了。
[csharp] view
plaincopyprint?
[System.Runtime.Serialization.DataContract]
public class Employee
{
/// <summary>
/// 员工姓名
/// </summary>
[System.Runtime.Serialization.DataMember(Name = "emp_name")]
public string Name { get; set; }
/// <summary>
/// 联系手机
/// </summary>
[System.Runtime.Serialization.DataMember(Name = "emp_phoneno")]
public string Phone { get; set; }
/// <summary>
/// 简介
/// </summary>
[System.Runtime.Serialization.DataMember(Name = "emp_desc")]
public string Description { get; set; }
}
如此一来,所保存的JSON数据就如下图所示:
转载:http://blog.csdn.net/tcjiaan/article/details/8191186
相关文章推荐
- 新时尚Windows8开发(33):Json数据处理(B)
- 新时尚Windows8开发(32):Json数据处理(A)
- 新时尚Windows8开发(32):Json数据处理(A)
- 新时尚Windows8开发(38):聊天程序
- 新时尚Windows8开发:那些关于按钮的事
- 新时尚Windows8开发(18):制作类似单行簿的输入控件
- 新时尚Windows8开发:ListBox与ComboBox
- 新时尚Windows8开发(31):去掉文本中的HTML标记
- 新时尚Windows8开发(20):FlipView控件
- 新时尚Windows8开发(13):如何选择保存文件
- 新时尚Windows8开发(29):多媒体捕捉(高级篇)
- 新时尚Windows8开发(22):ListBox与ComboBox
- 新时尚Windows8开发(12):如何选择并打开文件
- 实际开发中,后台回传的错误格式的Json数据处理
- 新时尚Windows8开发(15):扩展联系人选择器
- 新时尚Windows8开发(4):保存/读取本地应用程序设置
- 新时尚Windows8开发(9):加密和解密数据
- 新时尚Windows8开发(25):缩放视图
- 新时尚Windows8开发(40):StreamSocket的使用
- 【多端应用开发系列1.1.1 —— Android:使用新浪API V2】服务器Json数据处理——Json数据概述