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

一、JObject和JArray序列化

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

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            JArray array = new JArray();
            array.Add("GongHui Linq");
            array.Add(new DateTime(2015, 12, 14));

            JObject o = new JObject();
            o["myArray"] = array;

            string json = o.ToString();

            Console.WriteLine(json);
        }
    }
}


2.运行结果

  

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

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

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            JObject o = new JObject 
            {
                {"CPU","Intel"},
                {"Memory",2048},
                {
                    "Drives",new JArray
                    {
                        "DVD",
                        "U盘"
                    }
                }
            };

            Console.WriteLine(o.ToString());
        }
    }
}


2.运行结果

 

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

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

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JSONDemo
{
    public class Post
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Link { get; set; }
        public IList<string> Categories { get; set; }

        public Post()
        {
            Categories = new List<string>();
        }
    }
}


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

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Post p1=new Post();
            p1.Title="张五";
            p1.Description="张五的五一";
            p1.Link="http://www.zhuangwu.com";
            p1.Categories.Add("天地不仁");

            IList<Post> posts=new List<Post>();
            posts.Add(p1);

            JObject o = new JObject(
                new JProperty("channel",
                    new JObject(
                        new JProperty("title","龚辉"),
                        new JProperty("link","http://blog.csdn.net/lovegonghui/article/details/50293629"),
                        new JProperty("description","龚辉的博客"),
                        new JProperty("item",
                            new JArray(
                                from p in posts
                                orderby p.Title     
                                select new JObject(
                                    new JProperty("title",p.Title),
                                    new JProperty("description",p.Description),
                                    new JProperty("link",p.Link),
                                    new JProperty("categories",
                                        new JArray(
                                            from c in p.Categories
                                            select new JValue(c)))
                                        )
                                )
                            )
                    )
                )    
            );
            Console.WriteLine(o.ToString());
        }
    }
}

 

3.运行的结果

 

四、使用C#的dynamic序列化

1.创建一个对象Address.

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace JSONDemo
{
    public class Address
    {
        public string Province { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public IList<string> Villages { get; set; }        
    }
}


2.序列化

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {           

            dynamic address = new JObject();
            address.Province = "GuangDong";
            address.City = "GuangZhou";
            address.County = "PanYu";
            address.Villages = new JArray("大龙村", "小龙村");

            Console.WriteLine(address.ToString());
        }
    }
}


3.运行的结果

 

五、使用JTokenWriter序列化

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

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            JTokenWriter writer = new JTokenWriter();
            writer.WriteStartObject();
            writer.WritePropertyName("Title");
            writer.WriteValue("薄谷开来案???");

            writer.WritePropertyName("Detail");   
        
            writer.WriteStartArray();
            writer.WriteValue("Yes");
            writer.WriteValue("No");
            writer.WriteValue("Unknown");
            writer.WriteEndArray();

            writer.WriteEndObject();

            JObject o = (JObject)writer.Token;
            Console.WriteLine(o.ToString());
        }
    }
}


2.运行的结果

  

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

1.先创建一个Address对象.

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace JSONDemo
{
    public class Address
    {
        public string Province { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public IList<string> Villages { get; set; }        
    }
}


2.序列化操作

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            JValue i = (JValue)JToken.FromObject(123);
            Console.WriteLine(i.Type);
            Console.WriteLine(i.ToString());

            JValue s = (JValue)JToken.FromObject("GongHui");
            Console.WriteLine(s.Type);
            Console.WriteLine(s.ToString());

            Address address = new Address
            {
                City = "GuangZhou",
                Province = "GuangDong",
                County = "ShiQiao",
                Villages = new List<string>
                {
                    "维和",
                    "防稳"
                }
            };

            JObject o = (JObject)JToken.FromObject(address);
            Console.WriteLine(o.ToString());
        }
    }
}


3.运行结果

 

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

1.先创建一个Post对象

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JSONDemo
{
    public class Post
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Link { get; set; }
        public IList<string> Categories { get; set; }       
    }
}


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

[csharp] view plain copy
 
print?
  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. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json.Linq;

namespace JSONDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Post> posts = new List<Post>
            {
                new Post
                {
                    Title="匿名类型",
                    Description="匿名类型创建一个JObject",
                    Link="http://write.blog.csdn.net/postedit/50293629",
                    Categories=new List<string>
                    {
                        "JObject",
                        "匿名类型"
                    }
                }
            };

            JObject o = JObject.FromObject(new
            {
                channel = new
                {
                    title = "Linq的测试",
                    link = "http://www.microsoft/Linq.com",
                    description = "这是JOSN在Linq在的测试",
                    item =
                     from p in posts
                     orderby p.Title
                     select new 
                     {
                         title=p.Title,
                         link=p.Link,
                         description=p.Description,
                         categories=p.Categories
                     }
                }
            }
            );

            Console.WriteLine(o.ToString());
        }
    }
}


3.运行的结果

 原文:http://blog.csdn.net/lovegonghui/article/details/50293629

posted @ 2017-05-25 01:02  杨浪  阅读(6899)  评论(0编辑  收藏  举报