Metro应用Json数据处理

  Windows Phone 8 或者 Windows 8 平台对JSON数据的处理方式基本是一致的,需要使用DataContractJsonSerializer类将对象的实例序列化为JSON字符串,并将字符串反序列化为对象的实例,需要注意的此种处理方式要求Json数据的格式固定的。

  

  Json字符串 => 对象

  针对以下Json字符串进行数据对象的构造解析

{
    "name": "ZhangHui",
    "sex": "男",
    "cnblogs": {
        "address": "http://www.cnblogs.com/huizhang212/",
        "age": 2,
        "nickname": "宇之乐"
    },
    "contacts": [
        {
            "type": "QQ",
            "number": "411300114"
        },
        {
            "type": "Email",
            "number": "huizhang212@163.com"
        }
    ]
}

  1.构造对象

  分析上面json数据的格式,可以发现其中包含子对象cnblogs和子对象数组contacts,所以需要构造两个子对象Cnblogs和Contact,对象代码如下:

 1     /// <summary>
 2     /// 人员博客园论坛信息
 3     /// </summary>
 4     [DataContract]
 5     public class Cnblogs
 6     {
 7         [DataMember]
 8         public String nickname { get; set; }
 9         [DataMember]
10         public int age { get; set; }
11         [DataMember]
12         public String address { get; set; }
13     }
14 
15     /// <summary>
16     /// 人员电话号码信息对象
17     /// </summary>
18     [DataContract]
19     public class Contact
20     {
21         [DataMember]
22         public String type { get; set; }
23         [DataMember]
24         public String number { get; set; }
25     }

  子对象构建完成后,构建json数据对应的对象,记为:Person,代码如下:

 1     /// <summary>
 2     /// 人员信息对象
 3     /// </summary>
 4     [DataContract]
 5     public class Person
 6     {
 7         [DataMember]
 8         public String name { get; set; }
 9         [DataMember]
10         public String sex { get; set; }
11         [DataMember]
12         public Cnblogs cnblogs { get; set; }
13         [DataMember]
14         public List<Contact> contacts { get; set; }
15     }

  构造对象必须有[DataContract]声明,在通过DataContractJsonSerializer进行序列号或者反序列号的过程中只对有[DataMember]声明的属性生效,没有[DataMember]声明的属性将会被忽略,对象属性的大小写需要和json数据对应字段保持一致,否则该属性的值无法获取。

  2.Json字符串转换Person对象的转换函数实现

 1         /// <summary>
 2         /// 解析json数据转化为Person对象
 3         /// </summary>
 4         /// <param name="json"></param>
 5         /// <returns></returns>
 6         public Person getPersionByJson(String json)
 7         {
 8             if (String.IsNullOrEmpty(json))
 9             {
10                 return null;
11             }
12 
13             try
14             {
15                 Person info = new Person();
16                 MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
17                 DataContractJsonSerializer ser = new DataContractJsonSerializer(info.GetType());
18                 info = ser.ReadObject(ms) as Person;
19                 return info;
20             }
21             catch
22             {
23 
24             }
25             return null;
26         }

  3.调用getPersionByJson方法将Json字符串转换为对象

 1     //Json数据
 2     String json = 
 3            @"{
 4                 ""name"": ""ZhangHui"",
 5                 ""sex"": ""男"",
 6                 ""cnblogs"": {
 7                     ""address"": ""http://www.cnblogs.com/huizhang212/"",
 8                     ""age"": 2,
 9                     ""nickname"": ""宇之乐""
10                 },
11                 ""contacts"": [
12                     {
13                         ""type"": ""QQ"",
14                         ""number"": ""411300114""
15                     },
16                     {
17                         ""type"": ""Email"",
18                         ""number"": ""huizhang212@163.com""
19                     }
20                 ]
21             }";
22     //json数据转换为Person对象
23     Person person = getPersionByJson(json);

 

  对象 => Json字符串

  通过DataContractJsonSerializer可以直接将对象转换为Json字符串。

  1.Person对象转换Json字符串的函数实现

 1         /// <summary>
 2         /// 获Person对象的Json数据
 3         /// </summary>
 4         /// <param name="normal"></param>
 5         /// <returns></returns>
 6         public String getJsonByPerson(Person person)
 7         {
 8             String json = String.Empty;
 9             try
10             {
11                 MemoryStream ms = new MemoryStream();
12                 DataContractJsonSerializer ser = new DataContractJsonSerializer(person.GetType());
13                 ser.WriteObject(ms, person);
14                 ms.Position = 0;
15                 using (StreamReader s = new StreamReader(ms))
16                 {
17                     json = s.ReadToEnd();
18                 }
19             }
20             catch
21             {
22 
23             }
24             return json;
25         }

  2.调用getJsonByPerson方法将Person对象转换为Json字符串

 1     //构建一个Persion对象
 2     Person person = new Person()
 3     {
 4         name = "ZhangHui",
 5         sex = "",
 6         cnblogs = new Cnblogs()
 7         {
 8             nickname = "宇之乐",
 9             age = 2,
10             address = "http://www.cnblogs.com/huizhang212/"
11         },
12         contacts = new List<Contact>() 
13         {
14             new Contact(){ type="QQ", number="411300114" },
15             new Contact(){ type="Email", number="huizhang212@163.com" }
16         }
17     };
18     //转换为json数据
19     String personJson = getJsonByPerson(person);

 

posted @ 2013-11-15 14:09  宇之乐  阅读(1213)  评论(0编辑  收藏  举报