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
JSON中JObject和JArray,JValue序列化(Linq)的更多相关文章
- JSON中JObject和JArray的修改
一.JObject 和JArray的添加.修改.移除 1.先添加一个json字符串,把json字符串加载到JObject中,然后转换成JObject.根据索引修改对象的属性值,移除属性,添加属性 us ...
- json中jobject
Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...
- Json to JObject转换的使用方法
Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...
- Newtonsoft.Json中的时间格式详解.
Newtonsoft.json是一款不错的序列化反序列化第三方组件,具体如何使用属于基础知识,此处不再讲解.看以下代码: public class OutgameEntity { public str ...
- MVC3不能正确识别JSON中的Enum枚举值
一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...
- python全栈开发-json和pickle模块(数据的序列化)
一.什么是序列化? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serialization,marshalling,flat ...
- 读取Json,并替换json中的指定字符
string jsonfile = @"E:\history.json";//JSON文件路径 using (System.IO.FileStream file = new Fil ...
- 在JSON中遇到的一些坑
今天在进行压测的时候,由于需要使用到json进行传参,并且需要在JMeter中加入少量的JSON,由于JSON在java中呈现键值对的形式,并且需要使用到“”来修饰,导致只能使用\进行转义,在发送请求 ...
- json中获取key值
<script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...
随机推荐
- ios学习笔记(二)之Objective-C类、继承、类别和协议
二:Objective-C类与继承和协议 在前面已经提过了对象的初始化,这里首先讲的是变量. 2.1 变量 局部变量(内部变量): 局部变量是在方法内作定义说明的,其作用域仅限于方法内,离开方法后使用 ...
- MSSQL 清空日志 删除日志文件
MSSQL 清空日志 删除日志文件 最近的项目主要做数据的归档,把数据从一个数据库拉到另一个数据库,照成新数据库的日志文件非常大:于是想把日志文件删除.最简单就是先分离数据库->删除日志文件-& ...
- C# .Net 使用zxing.dll生成二维码,条形码
public static string GetBarcode(string format, string value, int? width, int? height) { ...
- hdu 1728 搜索求最少的转向次数
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- idea远程调试linux下的tomcat
要远程调试代码,首先的保障本地的代码和远程tomcat的代码是同一份 首先在本地idea配置一个远程tomcat服务器 host就填写远程主机ip port填写访问的端口(不是调试端口) 然后在Sta ...
- js区分汉字和字符,校验长度
遇到这么一个问题, 长度限制输入150个英文字符(小于等于150个英文字符长度),超出则直接禁止输入,并提醒:摘要输入必须小于等于75个中文字符长度! 长度校验倒是没问题,但是要区分汉字还是英文 ...
- jq向webApi提交post json数据
在页面想webApi post json数据的时候,发现webapi不能直接以json的方式接受数据(注:我是没有发现一个很好的方式来post json数据的);但是可以以数据结构的方式传递: 如下: ...
- VSFTP服务——实验
一.VSFTP 作用:提供文件共享服务,可以应用在互联网中,实现外地登录服务器下载公司文件的功能,不区分客户端,在windows和linux中都是可以使用的 1.安装vsftpd [root@Serv ...
- linux面试题集锦《转》
1. 下面的网络协议中,面向连接的的协议是: A . A 传输控制协议 B 用户数据报协议 C 网际协议 D 网际控制报文协议 2. 在/etc/fstab文件中指定的文件系统加载参数中, D 参数一 ...
- 如何使用jQuery动态的在body里添加script标签?
var script = document.createElement('script'); script.type = 'text/jacascript'; script.src = 'url'; ...