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

来源:互联网 发布:格力空调代码含义 知乎 编辑:程序博客网 时间:2024/05/17 03:12
一、JObject和JArray序列化

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

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#集合初始化语法,并序列化

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对象,添加构造函数。

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,然后声明一个对象列表。

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.

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.序列化

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写入属性与值,数组。

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对象.

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.序列化操作

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对象

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

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.运行的结果

 

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

2 0