你的位置:首页 > 操作系统

[操作系统]windowsphone8.1学习笔记之应用数据(四)


应用数据的存储格式常用的分为json和

先说说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();    }//读取jsonJsonObject jsonObject = JsonObject.Parse(jsonString);//第二参数为默认值var name = jsonObject.GetNamedString("Name","");var age = jsonObject.GetNamedNumber("Name",0);

以上就是json序列化和反序列化,接下来简单说说

public async void ObjectTostring 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> 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 };      ObjectTo<People>(p, "testSerializer");      var file = await ApplicationData.Current.LocalFolder.GetFileAsync("testSerializer");      var str = await FileIO.ReadTextAsync(file);

str的值如下: 

<People "http://www.w3.org/2001/" "http://schemas.datacontract.org/2004/07/App1"><Age>11</Age><Name>waha</Name></People>

应用数据的存储格式就说到这里,整个应用数据的内容也就完了,晚安!