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

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

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

[csharp] view plaincopy
  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对象,添加构造函数。

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

[csharp] view plaincopy
  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.

[csharp] view plaincopy
  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.序列化

[csharp] view plaincopy
  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写入属性与值,数组。

[csharp] view plaincopy
  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对象.

[csharp] view plaincopy
  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.序列化操作

[csharp] view plaincopy
  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对象

[csharp] view plaincopy
  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

[csharp] view plaincopy
  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

转载于:https://www.cnblogs.com/zcm123/p/6144036.html

JSON中JObject和JArray,JValue序列化(Linq)相关推荐

  1. ASP.NET中使用JObject和JArray解析Json数据 (实用、赞)

    原文出处:ASP.NET中使用JObject和JArray解析Json数据 - 谢友海 - 博客园 本章将和大家分享如何在ASP.NET中使用JObject和JArray解析Json数据.话不多说,下 ...

  2. ASP.NET中使用JObject和JArray解析Json数据

    本章将和大家分享如何在ASP.NET中使用JObject和JArray解析Json数据.话不多说,下面我们直接来看一个示例. 数据样例(模拟接口返回的Json字符串),如下所示: {"cod ...

  3. c#: Newtonsoft.Json 高级用法一(不创建类,动态解析和构造json、JObject/JArray)

    环境: .net core3.1 vs2019 Newtonsoft.Json 12.0.3 关于newtonsoft.json的使用常见问题参考: <c#:序列化json常见问题及处理方法&g ...

  4. Newtonsoft.Json - JObject与JArray总结

    Newtonsoft.Json是一款.net下的Json序列化/反序列化库,省去了手动拼Json的麻烦,可以通过官网或者NuGet下载. JObject是其中比较万金油的一个类,可以在不使用实体类的情 ...

  5. .NET 6 新特性 System.Text.Json 中的 Writeable DOM

    .NET 6 新特性 System.Text.Json 中的 Writeable DOM 特性 Intro 在 .NET 6 Preview 4 中,微软加入了 JSON Node 的支持,我们可以动 ...

  6. @JsonIgnoreProperties转换实体时忽略json中不存在的字段

    开发时遇见这么一个情况,对接放发出的json格式不确定,这里的不确定是json中的字段不确定,以往都是采用gson进行实体和json的转换,但是找了挺长时间,还是没找到gson中可以解决这个情况的办法 ...

  7. json中omitempty字段的使用

    总结 1. omitempty是省略的意思 2. json中字段若有omitempty标记,则这个字段为空时,json序列化为string时不会包含该字段 3. json中字段若没有omitempty ...

  8. 在Dubbo中使用高效的Java序列化(Kryo和FST)

    作者:沈理 文档版权: Apache 2.0许可证 署名-禁止演绎 完善中-- TODO 生成可点击的目录 目录 序列化漫谈 启用Kryo和FST 注册被序列化类 无参构造函数和Serializabl ...

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

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

  10. System.Text.Json 中的字符编码

    System.Text.Json 中的字符编码 Intro 默认的 System.Text.Json 序列化的时候会把所有的非 ASCII 的字符进行转义,这就会导致很多时候我们的一些非 ASCII ...

最新文章

  1. Tomcat备份脚本
  2. abaqus最大应力准则怎么用_ANSYS与ABAQUS对比,你选择那个?
  3. Node.js event loop 和 JS 浏览器环境下的事件循环的区别
  4. python原始web与django框架 mvc模式开发
  5. 逻辑漏洞之修改响应包绕过登录校验
  6. [C语言]为什么要有include?——从Hello World说起
  7. python做自动化控制postman_python自动化测试入门篇-postman
  8. [转载].一直不怎么明白PID的运算输出结果怎么换算成执行机构的控制量
  9. brave浏览器_据说只有这款浏览器,真正做到了保护隐私
  10. MATLAB2017安装步骤
  11. matlab2010安装详细图解案例
  12. 安卓 文本框怎么贴近边缘_【安卓,iOS】全网最火的充电提示音教程来啦
  13. 第十篇:SpringBoot集成支付宝接口扫码支付
  14. SpringMVC的数据请求
  15. 基于C语言实现的SML简单程序设计
  16. ubuntu16.04下安装新版QQ
  17. pdfh5使用及不显示pdf文件原因
  18. Maven传递性依赖解读
  19. (数字ic)CDC设计实例 - ICG :integrate Clock Gating Cell
  20. 自学软件测试,学到什么程度可以出去找工作啊?

热门文章

  1. Safari浏览器Session问题
  2. [FZYZOJ 1889] 厨房救济
  3. bzoj2038 [2009国家集训队]小Z的袜子(hose)
  4. 安装Lua For Windows
  5. mysql cluster 子查询速度很慢
  6. Mysql(8)_存储引擎之InnoDB
  7. FineUI分组显示弹框最新的在最上边
  8. js中的Math对象及属性
  9. 第一回合:.net与 C#基本概念
  10. Xml序列化和反序列化对象-使用MemoryStream-实践