windowsphone8.1学习笔记之应用数据(四)
2015-08-22 23:46
369 查看
应用数据的存储格式常用的分为json和xml两种(其实我都想略过这个地方的,json我一直用的是json.net,而wp上操作xml的方式与其他相比也没太多变化)。
先说说json数据存储,关于json概念就不粘贴复制了。在wp里使用json数据格式来存储相关的信息会有两种编程方式:DataContractJsonSerializer类和JsonObject。
DataContractJsonSerializer类对Json数据进行序列化和反序列化是最简洁的Json数据操作方式,序列化的过程是把实体类对象转化为json字符串对象,该操作是直接把实体类的属性名称和属性值组成“名称/值”的形式,反序列化的过程则刚好倒过来。
以上就是DataContractJsonSerializer类序列化和反序列化的方法,接下来说说JsonObject类自定义json对象。
以上就是json序列化和反序列化,接下来简单说说xml的操作,DataContractSerializer类和XmlDocument类(其实我用的较多的是XDocument)。先说说DataContractSerializer类,这个的名称和上面的比较相似,就是少了个“Json”。
测试如下:
str的值如下:
应用数据的存储格式就说到这里,整个应用数据的内容也就完了,晚安!
先说说json数据存储,关于json概念就不粘贴复制了。在wp里使用json数据格式来存储相关的信息会有两种编程方式:DataContractJsonSerializer类和JsonObject。
DataContractJsonSerializer类对Json数据进行序列化和反序列化是最简洁的Json数据操作方式,序列化的过程是把实体类对象转化为json字符串对象,该操作是直接把实体类的属性名称和属性值组成“名称/值”的形式,反序列化的过程则刚好倒过来。
public string DataToJson(object obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); string result = string.Empty; using(MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, obj); ms.Position = 0; using(StreamReader reader = new StreamReader(ms)) { result = reader.ReadToEnd(); } } return result; } public T JsonToData<T>(string jsonString) { var ds = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); T obj = (T)ds.ReadObject(ms); ms.Dispose(); return obj; }
以上就是DataContractJsonSerializer类序列化和反序列化的方法,接下来说说JsonObject类自定义json对象。
//组装json串 //jsonObject.SetNameString("Name",JsonValue.CreateStringValue("waha")) public string GetJsonByJsonObject() { JsonObject jsonObject = new JsonObject(); jsonObject["Name"] = JsonValue.CreateStringValue("waha"); jsonObject["Age"] = JsonValue.CreateNumberValue(11); return jsonObject.Stringify(); } //读取json JsonObject jsonObject = JsonObject.Parse(jsonString); //第二参数为默认值 var name = jsonObject.GetNamedString("Name",""); var age = jsonObject.GetNamedNumber("Name",0);
以上就是json序列化和反序列化,接下来简单说说xml的操作,DataContractSerializer类和XmlDocument类(其实我用的较多的是XDocument)。先说说DataContractSerializer类,这个的名称和上面的比较相似,就是少了个“Json”。
public async void ObjectToXml<T>(T data,string fileName) { StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) using (IOutputStream outStream = stream.GetOutputStreamAt(0)) { DataContractSerializer serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(outStream.AsStreamForWrite(), data); await outStream.FlushAsync(); } } public async Task<T> XmlToObject<T>(string filename) { T _sessionState = default(T); StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename); if (file == null) return _sessionState; using (IInputStream inStream = await file.OpenSequentialReadAsync()) { DataContractSerializer serializer = new DataContractSerializer(typeof(T)); _sessionState = (T)serializer.ReadObject(inStream.AsStreamForRead()); } return _sessionState; }
测试如下:
People p = new People { Name = "waha", Age = 11 }; ObjectToXml<People>(p, "testSerializer"); var file = await ApplicationData.Current.LocalFolder.GetFileAsync("testSerializer"); var str = await FileIO.ReadTextAsync(file);
str的值如下:
<People xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/App1"><Age>11</Age><Name>waha</Name></People>
应用数据的存储格式就说到这里,整个应用数据的内容也就完了,晚安!
相关文章推荐
- OC学习日记-继承,多态,私有属性、变量和方法,self,super,属性修饰符,description
- andrid面试题
- andrid面试题
- ios中字符串的一些常见操作
- JPA的api主要类和接口
- andrid面试题
- NOI2004郁闷的出纳员bzoj3503
- 洛谷1434 滑雪
- Aizu - 2555 Everlasting Zero 模拟
- Linux简单命令学习
- Visual Studio 2013:打开cordova出现WebSocket4Net missing from your system
- 自制 Python3爬虫
- ntp/系统时钟/硬件时钟/双系统下计算机时间读取的问题
- UVALive - 4960 Sensor network(生成树+LCA)
- Fiddler拦截Chrome浏览器请求设置
- 由第三方软件or硬件提供商导致的oracle database 损坏问题
- Python网络编程(2)——socket模块(2)
- android学习,BroadcastReceiver
- 有趣的C/C++语言
- Eclipse中Maven添加web项目