JSON序列化和反序列化集合、字典、数组的处理

在JSON数据中,所有的集合、字典和数组都表示为数组。

    

     List序列化:

   1: List list = new List()
   2: {
   3:     new Person(){ Name="张三", Age=28},
   4:     new Person(){ Name="李四", Age=25}
   5: };
   6:  
   7: string jsonString = JsonHelper.JsonSerializer>(list);

     序列化结果:

"[{/"Age/":28,/"Name/":/"张三/"},{/"Age/":25,/"Name/":/"李四/"}]"

    字典不能直接用于JSON,Dictionary字典转化为JSON并不是跟原来的字典格式一致,而是形式以Dictionary的Key作为名称”Key“的值,以Dictionary的Value作为名称为”Value“的值 。如:

   1: Dictionary<string, string> dic = new Dictionary<string, string>();
   2: dic.Add("Name", "张三");
   3: dic.Add("Age", "28");
   4:  
   5: string jsonString = JsonHelper.JsonSerializer < Dictionary<string, string>>(dic);

      序列化结果:

   1: "[{/"Key/":/"Name/",/"Value/":/"张三/"},{/"Key/":/"Age/",/"Value/":/"28/"}]"

 

 

JSON官网:http://www.json.org/json-zh.html

独立JSON序列化:http://msdn.microsoft.com/zh-cn/library/bb412170.aspx

如何对JSON序列化和反序列化:http://msdn.microsoft.com/zh-cn/library/bb412179.aspx

你可能感兴趣的:(JSON序列化和反序列化集合、字典、数组的处理)