JSON中JObject和JArray,JValue序列化(Linq)

一、JObject和JArray序列化

1.实例化JArray和JObject,然后序列化

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json.Linq;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             JArray array = new JArray();  
  15.             array.Add("GongHui Linq");  
  16.             array.Add(new DateTime(2015, 12, 14));  
  17.   
  18.             JObject o = new JObject();  
  19.             o["myArray"] = array;  
  20.   
  21.             string json = o.ToString();  
  22.   
  23.             Console.WriteLine(json);  
  24.         }  
  25.     }  
  26. }  


2.运行结果

  

二、JObject和JArray使用C#集合初始化语法序列化

1.使用C#集合初始化语法,并序列化

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json.Linq;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             JObject o = new JObject   
  15.             {  
  16.                 {"CPU","Intel"},  
  17.                 {"Memory",2048},  
  18.                 {  
  19.                     "Drives",new JArray  
  20.                     {  
  21.                         "DVD",  
  22.                         "U盘"  
  23.                     }  
  24.                 }  
  25.             };  
  26.   
  27.             Console.WriteLine(o.ToString());  
  28.         }  
  29.     }  
  30. }  


2.运行结果

三、使用Linq创建JObject和JArray序列化

1.创建一个Post对象,添加构造函数。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace JSONDemo  
  7. {  
  8.     public class Post  
  9.     {  
  10.         public string Title { get; set; }  
  11.         public string Description { get; set; }  
  12.         public string Link { get; set; }  
  13.         public IList<string> Categories { get; set; }  
  14.   
  15.         public Post()  
  16.         {  
  17.             Categories = new List<string>();  
  18.         }  
  19.     }  
  20. }  


2.实例化Post,然后声明一个对象列表。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json.Linq;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             Post p1=new Post();  
  15.             p1.Title="张五";  
  16.             p1.Description="张五的五一";  
  17.             p1.Link="http://www.zhuangwu.com";  
  18.             p1.Categories.Add("天地不仁");  
  19.   
  20.             IList<Post> posts=new List<Post>();  
  21.             posts.Add(p1);  
  22.   
  23.             JObject o = new JObject(  
  24.                 new JProperty("channel",  
  25.                     new JObject(  
  26.                         new JProperty("title","龚辉"),  
  27.                         new JProperty("link","http://blog.csdn.net/lovegonghui/article/details/50293629"),  
  28.                         new JProperty("description","龚辉的博客"),  
  29.                         new JProperty("item",  
  30.                             new JArray(  
  31.                                 from p in posts  
  32.                                 orderby p.Title       
  33.                                 select new JObject(  
  34.                                     new JProperty("title",p.Title),  
  35.                                     new JProperty("description",p.Description),  
  36.                                     new JProperty("link",p.Link),  
  37.                                     new JProperty("categories",  
  38.                                         new JArray(  
  39.                                             from c in p.Categories  
  40.                                             select new JValue(c)))  
  41.                                         )  
  42.                                 )  
  43.                             )  
  44.                     )  
  45.                 )      
  46.             );  
  47.             Console.WriteLine(o.ToString());  
  48.         }  
  49.     }  
  50. }  

3.运行的结果

四、使用C#的dynamic序列化

1.创建一个对象Address.

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ComponentModel;  
  6.   
  7. namespace JSONDemo  
  8. {  
  9.     public class Address  
  10.     {  
  11.         public string Province { get; set; }  
  12.         public string City { get; set; }  
  13.         public string County { get; set; }  
  14.         public IList<string> Villages { get; set; }          
  15.     }  
  16. }  


2.序列化

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json.Linq;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {             
  14.   
  15.             dynamic address = new JObject();  
  16.             address.Province = "GuangDong";  
  17.             address.City = "GuangZhou";  
  18.             address.County = "PanYu";  
  19.             address.Villages = new JArray("大龙村", "小龙村");  
  20.   
  21.             Console.WriteLine(address.ToString());  
  22.         }  
  23.     }  
  24. }  


3.运行的结果

五、使用JTokenWriter序列化

1.首先使用JTokenWriter写入属性与值,数组。

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json.Linq;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             JTokenWriter writer = new JTokenWriter();  
  15.             writer.WriteStartObject();  
  16.             writer.WritePropertyName("Title");  
  17.             writer.WriteValue("薄谷开来案???");  
  18.   
  19.             writer.WritePropertyName("Detail");     
  20.           
  21.             writer.WriteStartArray();  
  22.             writer.WriteValue("Yes");  
  23.             writer.WriteValue("No");  
  24.             writer.WriteValue("Unknown");  
  25.             writer.WriteEndArray();  
  26.   
  27.             writer.WriteEndObject();  
  28.   
  29.             JObject o = (JObject)writer.Token;  
  30.             Console.WriteLine(o.ToString());  
  31.         }  
  32.     }  
  33. }  


2.运行的结果

  

六、使用JToken.FromObject(object)把.NET值转换成JSON中Linq序列化

1.先创建一个Address对象.

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ComponentModel;  
  6.   
  7. namespace JSONDemo  
  8. {  
  9.     public class Address  
  10.     {  
  11.         public string Province { get; set; }  
  12.         public string City { get; set; }  
  13.         public string County { get; set; }  
  14.         public IList<string> Villages { get; set; }          
  15.     }  
  16. }  


2.序列化操作

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json.Linq;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             JValue i = (JValue)JToken.FromObject(123);  
  15.             Console.WriteLine(i.Type);  
  16.             Console.WriteLine(i.ToString());  
  17.   
  18.             JValue s = (JValue)JToken.FromObject("GongHui");  
  19.             Console.WriteLine(s.Type);  
  20.             Console.WriteLine(s.ToString());  
  21.   
  22.             Address address = new Address  
  23.             {  
  24.                 City = "GuangZhou",  
  25.                 Province = "GuangDong",  
  26.                 County = "ShiQiao",  
  27.                 Villages = new List<string>  
  28.                 {  
  29.                     "维和",  
  30.                     "防稳"  
  31.                 }  
  32.             };  
  33.   
  34.             JObject o = (JObject)JToken.FromObject(address);  
  35.             Console.WriteLine(o.ToString());  
  36.         }  
  37.     }  
  38. }  


3.运行结果

七、匿名类型创建一个JObject序列化

1.先创建一个Post对象

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace JSONDemo  
  7. {  
  8.     public class Post  
  9.     {  
  10.         public string Title { get; set; }  
  11.         public string Description { get; set; }  
  12.         public string Link { get; set; }  
  13.         public IList<string> Categories { get; set; }         
  14.     }  
  15. }  


2.实例化对象Post,然后使用JObject.FromObject(object)创建一个匿名类型对象channel

[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json.Linq;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             List<Post> posts = new List<Post>  
  15.             {  
  16.                 new Post  
  17.                 {  
  18.                     Title="匿名类型",  
  19.                     Description="匿名类型创建一个JObject",  
  20.                     Link="http://write.blog.csdn.net/postedit/50293629",  
  21.                     Categories=new List<string>  
  22.                     {  
  23.                         "JObject",  
  24.                         "匿名类型"  
  25.                     }  
  26.                 }  
  27.             };  
  28.   
  29.             JObject o = JObject.FromObject(new  
  30.             {  
  31.                 channel = new  
  32.                 {  
  33.                     title = "Linq的测试",  
  34.                     link = "http://www.microsoft/Linq.com",  
  35.                     description = "这是JOSN在Linq在的测试",  
  36.                     item =  
  37.                      from p in posts  
  38.                      orderby p.Title  
  39.                      select new   
  40.                      {  
  41.                          title=p.Title,  
  42.                          link=p.Link,  
  43.                          description=p.Description,  
  44.                          categories=p.Categories  
  45.                      }  
  46.                 }  
  47.             }  
  48.             );  
  49.   
  50.             Console.WriteLine(o.ToString());  
  51.         }  
  52.     }  
  53. }  


3.运行的结果

JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751

原文地址:https://www.cnblogs.com/zxtceq/p/7910142.html