JSON中JObject和JArray,JValue序列化(Linq)
来源:互联网 发布:格力空调代码含义 知乎 编辑:程序博客网 时间:2024/05/17 03:12
一、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
2 0
- JSON中JObject和JArray,JValue序列化(Linq)
- JSON中JObject和JArray的修改
- Newtonsoft.Json解析,JObject、JArray的联系
- 遍历Newtonsoft.Json.Linq.JObject
- JArray ,JObject 及反序列化实体类等问题
- JSON中使用JArray.Parse
- Newtonsoft.Json.Linq.JArray 转换成 List<T>
- C# JArray与JObject 的使用
- Json.net/Newtonsoft 3.0 新特性JObject/Linq to Json
- Json.net/Newtonsoft 新特性JObject/Linq to Json
- 反序列化JSON字符串 &遍历JObject
- Jobject获取json集合
- JSON 解析--JObject
- JSON 解析--JObject
- 序列化和json
- 序列化和json
- Linq To XML 和 序列化XML
- C# 基础,JOject JArray构造json对象
- VS2015 ATL 在XP 系统下无法注册
- Android Context完全解析,你所不知道的Context的各种细节
- Javascript中Event属性大全
- gettid 和pthread_self的区别
- VGA接口时序练习之图像动态显示
- JSON中JObject和JArray,JValue序列化(Linq)
- 如何打造一个可以无限注册账号的手机
- linux编译boost
- 互联网推送服务原理:长连接+心跳机制(MQTT协议)
- Spring学习笔记(十三):jsp、freemarker、velocity的区别
- Objective-c语言_基本数据类型封装和拆包
- 解决Xcode 9.2系统真机测试时出现 could not find developer disk image问题
- Java线程面试题 Top 50
- android 自动生成ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi图片的工具