JSON使用TypeNameHandling序列化与反序列化.NET类型和声明类型
来源:互联网 发布:vod电影点播软件 编辑:程序博客网 时间:2024/06/05 22:53
1.先创建一个抽象类WildAnimal,再创建一个派生类Panda,最后创建一个含有抽象类集合的PandaInfo类.
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace JSONDemo{ public abstract class WildAnimal { public string Name { get; set; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace JSONDemo{ public class Panda : WildAnimal { public string Food { get; set; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace JSONDemo{ public class PandaInfo { public int Lifetime { get; set; } public IList<WildAnimal> WildAnimal { get; set; } }}
2.序列化与反序列化
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using GongHuiNewtonsoft.Json;namespace JSONDemo{ class Program { static void Main(string[] args) { PandaInfo info = new PandaInfo() { Lifetime = 25, WildAnimal = new List<WildAnimal> { new Panda { Name="Meimei", Food="bamboo" } } }; Console.WriteLine("----------序列化包含声明类型与.NET类型------------"); string all = JsonConvert.SerializeObject(info, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); Console.WriteLine(all); Console.WriteLine("--------------序列化包含.NET类型------------------"); string auto = JsonConvert.SerializeObject(info, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); Console.WriteLine(auto); Console.WriteLine("--------------反序列化包含.NET类型----------------"); PandaInfo info2 = JsonConvert.DeserializeObject<PandaInfo>(auto, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); Console.WriteLine(info2.Lifetime); Panda p = info2.WildAnimal[0] as Panda; Console.WriteLine(p.Food); Console.WriteLine(p.Name); } }}
3.运行的结果
JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
0 0
- JSON使用TypeNameHandling序列化与反序列化.NET类型和声明类型
- JSON使用Linq序列化与反序列化.NET类型
- JSON序列化与反序列化匿名类型
- C# json的序列化与反序列化 字典集合,类型集合
- C# dynamic类型序列化和反序列化之Newtonsoft.Json
- C# 复杂类型序列化与反序列化
- .Net学习笔记 - 参数类型的序列化与反序列化
- 在MVC中使用Json.Net序列化和反序列化Json对象
- ASP.NET:使用Newtonsoft.Json序列化和反序列化JSON对象的例子
- ASP.NET的JSON序列化与反序列化
- 类型的序列化和反序列化
- 基本类型序列化和反序列化
- .NET进行Json的序列化和反序列化
- ASP.NET Json序列化和反序列化实例
- .NET JSON对象序列化和反序列化
- json.net 对象序列化和反序列化
- .Net中JSON序列化和反序列化
- .net对json序列化和反序列化
- Bonfire: Falsy Bouncer
- aerospike工具集——AMC(管理控制台)
- Didn't find class "android.support.v7.widget.RecyclerView"
- ORA-24324 ORA-01041
- zhphp framework (四)动态加载应用程序产量,方便任何程序代码重构
- JSON使用TypeNameHandling序列化与反序列化.NET类型和声明类型
- QMutex 类学习笔记
- ubuntu安装openssh-server报依赖错误
- show_ip.sh
- aerospike工具集——Command-Line Utility (cli)
- 王海盯上京东,假货问题是本质
- JAVA性能调优之基本调优策略和手段
- Word文档中插入高亮代码完美实现
- RecyclerView Item 的点击事件