JSON中JObject和JArray,JValue序列化(Linq)
http://blog.csdn.net/lovegonghui/article/details/50293629
一、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
转载于:https://www.cnblogs.com/zcm123/p/6144036.html
JSON中JObject和JArray,JValue序列化(Linq)相关推荐
- ASP.NET中使用JObject和JArray解析Json数据 (实用、赞)
原文出处:ASP.NET中使用JObject和JArray解析Json数据 - 谢友海 - 博客园 本章将和大家分享如何在ASP.NET中使用JObject和JArray解析Json数据.话不多说,下 ...
- ASP.NET中使用JObject和JArray解析Json数据
本章将和大家分享如何在ASP.NET中使用JObject和JArray解析Json数据.话不多说,下面我们直接来看一个示例. 数据样例(模拟接口返回的Json字符串),如下所示: {"cod ...
- c#: Newtonsoft.Json 高级用法一(不创建类,动态解析和构造json、JObject/JArray)
环境: .net core3.1 vs2019 Newtonsoft.Json 12.0.3 关于newtonsoft.json的使用常见问题参考: <c#:序列化json常见问题及处理方法&g ...
- Newtonsoft.Json - JObject与JArray总结
Newtonsoft.Json是一款.net下的Json序列化/反序列化库,省去了手动拼Json的麻烦,可以通过官网或者NuGet下载. JObject是其中比较万金油的一个类,可以在不使用实体类的情 ...
- .NET 6 新特性 System.Text.Json 中的 Writeable DOM
.NET 6 新特性 System.Text.Json 中的 Writeable DOM 特性 Intro 在 .NET 6 Preview 4 中,微软加入了 JSON Node 的支持,我们可以动 ...
- @JsonIgnoreProperties转换实体时忽略json中不存在的字段
开发时遇见这么一个情况,对接放发出的json格式不确定,这里的不确定是json中的字段不确定,以往都是采用gson进行实体和json的转换,但是找了挺长时间,还是没找到gson中可以解决这个情况的办法 ...
- json中omitempty字段的使用
总结 1. omitempty是省略的意思 2. json中字段若有omitempty标记,则这个字段为空时,json序列化为string时不会包含该字段 3. json中字段若没有omitempty ...
- 在Dubbo中使用高效的Java序列化(Kryo和FST)
作者:沈理 文档版权: Apache 2.0许可证 署名-禁止演绎 完善中-- TODO 生成可点击的目录 目录 序列化漫谈 启用Kryo和FST 注册被序列化类 无参构造函数和Serializabl ...
- python全栈开发-json和pickle模块(数据的序列化)
一.什么是序列化? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serialization,marshalling,flat ...
- System.Text.Json 中的字符编码
System.Text.Json 中的字符编码 Intro 默认的 System.Text.Json 序列化的时候会把所有的非 ASCII 的字符进行转义,这就会导致很多时候我们的一些非 ASCII ...
最新文章
- Tomcat备份脚本
- abaqus最大应力准则怎么用_ANSYS与ABAQUS对比,你选择那个?
- Node.js event loop 和 JS 浏览器环境下的事件循环的区别
- python原始web与django框架 mvc模式开发
- 逻辑漏洞之修改响应包绕过登录校验
- [C语言]为什么要有include?——从Hello World说起
- python做自动化控制postman_python自动化测试入门篇-postman
- [转载].一直不怎么明白PID的运算输出结果怎么换算成执行机构的控制量
- brave浏览器_据说只有这款浏览器,真正做到了保护隐私
- MATLAB2017安装步骤
- matlab2010安装详细图解案例
- 安卓 文本框怎么贴近边缘_【安卓,iOS】全网最火的充电提示音教程来啦
- 第十篇:SpringBoot集成支付宝接口扫码支付
- SpringMVC的数据请求
- 基于C语言实现的SML简单程序设计
- ubuntu16.04下安装新版QQ
- pdfh5使用及不显示pdf文件原因
- Maven传递性依赖解读
- (数字ic)CDC设计实例 - ICG :integrate Clock Gating Cell
- 自学软件测试,学到什么程度可以出去找工作啊?