一、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);
- }
- }
- }
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());
- }
- }
- }
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>();
- }
- }
- }
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());
- }
- }
- }
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; }
- }
- }
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());
- }
- }
- }
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());
- }
- }
- }
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; }
- }
- }
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());
- }
- }
- }
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; }
- }
- }
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());
- }
- }
- }
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