http://blog.csdn.net/lovegonghui/article/details/50293629
一、JObject和JArray序列化

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

  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. namespace JSONDemo
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. JArray array = new JArray();
  14. array.Add("GongHui Linq");
  15. array.Add(new DateTime(2015, 12, 14));
  16. JObject o = new JObject();
  17. o["myArray"] = array;
  18. string json = o.ToString();
  19. Console.WriteLine(json);
  20. }
  21. }
  22. }

2.运行结果

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

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

  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. namespace JSONDemo
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. JObject o = new JObject
  14. {
  15. {"CPU","Intel"},
  16. {"Memory",2048},
  17. {
  18. "Drives",new JArray
  19. {
  20. "DVD",
  21. "U盘"
  22. }
  23. }
  24. };
  25. Console.WriteLine(o.ToString());
  26. }
  27. }
  28. }

2.运行结果

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

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

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

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

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

3.运行的结果

四、使用C#的dynamic序列化

1.创建一个对象Address.

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

2.序列化

  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. namespace JSONDemo
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. dynamic address = new JObject();
  14. address.Province = "GuangDong";
  15. address.City = "GuangZhou";
  16. address.County = "PanYu";
  17. address.Villages = new JArray("大龙村", "小龙村");
  18. Console.WriteLine(address.ToString());
  19. }
  20. }
  21. }

3.运行的结果

五、使用JTokenWriter序列化

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

  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. namespace JSONDemo
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. JTokenWriter writer = new JTokenWriter();
  14. writer.WriteStartObject();
  15. writer.WritePropertyName("Title");
  16. writer.WriteValue("薄谷开来案???");
  17. writer.WritePropertyName("Detail");
  18. writer.WriteStartArray();
  19. writer.WriteValue("Yes");
  20. writer.WriteValue("No");
  21. writer.WriteValue("Unknown");
  22. writer.WriteEndArray();
  23. writer.WriteEndObject();
  24. JObject o = (JObject)writer.Token;
  25. Console.WriteLine(o.ToString());
  26. }
  27. }
  28. }

2.运行的结果

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

1.先创建一个Address对象.

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

2.序列化操作

  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. namespace JSONDemo
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. JValue i = (JValue)JToken.FromObject(123);
  14. Console.WriteLine(i.Type);
  15. Console.WriteLine(i.ToString());
  16. JValue s = (JValue)JToken.FromObject("GongHui");
  17. Console.WriteLine(s.Type);
  18. Console.WriteLine(s.ToString());
  19. Address address = new Address
  20. {
  21. City = "GuangZhou",
  22. Province = "GuangDong",
  23. County = "ShiQiao",
  24. Villages = new List<string>
  25. {
  26. "维和",
  27. "防稳"
  28. }
  29. };
  30. JObject o = (JObject)JToken.FromObject(address);
  31. Console.WriteLine(o.ToString());
  32. }
  33. }
  34. }

3.运行结果

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

1.先创建一个Post对象

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

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

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

3.运行的结果

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

JSON中JObject和JArray,JValue序列化(Linq)的更多相关文章

  1. JSON中JObject和JArray的修改

    一.JObject 和JArray的添加.修改.移除 1.先添加一个json字符串,把json字符串加载到JObject中,然后转换成JObject.根据索引修改对象的属性值,移除属性,添加属性 us ...

  2. json中jobject

    Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...

  3. Json to JObject转换的使用方法

    Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...

  4. Newtonsoft.Json中的时间格式详解.

    Newtonsoft.json是一款不错的序列化反序列化第三方组件,具体如何使用属于基础知识,此处不再讲解.看以下代码: public class OutgameEntity { public str ...

  5. MVC3不能正确识别JSON中的Enum枚举值

    一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...

  6. python全栈开发-json和pickle模块(数据的序列化)

    一.什么是序列化? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serialization,marshalling,flat ...

  7. 读取Json,并替换json中的指定字符

    string jsonfile = @"E:\history.json";//JSON文件路径 using (System.IO.FileStream file = new Fil ...

  8. 在JSON中遇到的一些坑

    今天在进行压测的时候,由于需要使用到json进行传参,并且需要在JMeter中加入少量的JSON,由于JSON在java中呈现键值对的形式,并且需要使用到“”来修饰,导致只能使用\进行转义,在发送请求 ...

  9. json中获取key值

    <script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...

随机推荐

  1. MVC4.0系统开发新手历程1

    MVC4.0系统开发新手历程(一) 接手了一个简单的销售奖金计算的项目,虽然不算大但是业务逻辑比较复杂,还夹杂了很多的特殊情况,毕竟是大公司什么样的人都有,好了不多说切入正题,项目是公司的一个前辈负责 ...

  2. 依赖注入(IOC)二

    依赖注入(IOC)二 上一章我们讲了构造注入与设值注入,这一篇我们主要讲接口注入与特性注入. 接口注入 接口注入是将抽象类型的入口以方法定义在一个接口中,如果客户类型需要获得这个方法,就需要以实现这个 ...

  3. iOS多线程的初步研究

    iOS多线程的初步研究(四) 理解run loop后,才能彻底理解NSTimer的实现原理,也就是说NSTimer实际上依赖run loop实现的. 先看看NSTimer的两个常用方法: + (NST ...

  4. OC-变量和数据类型

    对象的初始化 Fraction *myFract=[[Fraction alloc] init];//初始化对象 [myFract setTo:1 over:3];//设置初始值 初始化对象和设置初始 ...

  5. jQuery的delegate

    jQuery的delegate 在网页开发的过程中经常遇到的一个需求就是点击一div内部做某些操作,而点击页面其它地方隐藏该div.比如很多导航菜单,当菜单展开的时候,就会要求点击页面其它非菜单地方, ...

  6. 由IEnumerable和IEnumerator的延伸

    相信大家在学习c#的时候,经常会看到IEnumerable.IEnumerator这样的接口或返回类型,在写代码时经常会对数组或List集合进行遍历.那IEnumerable和IEnumerator是 ...

  7. Klockwork告警常见错误

    下面列举的是Klockwork告警中常见的告警形式,这些情况在编译阶段都不会报出来语法上的错误,并且在运行阶段执行到的概率很小.但是在某些场景下一旦执行到了这些语句, 很可能引起进程的跑飞和挂起.   ...

  8. 线程内唯一对象HttpContext

    在asp.net中,HttpContext是主线程内唯一对象.在web应用中开启多线程,在另外一个线程中是无法访问HttpContext. 如果需要在另外一个线程中使用HttpContext.Curr ...

  9. 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置

    在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...

  10. dede搜索引擎

    1.dede模板中的html: <form action="{dede:fieldname='phpurl'/}/search.php" name="formsea ...