JSON使用DefaultSettings序列化与反序列化
2015-12-14 10:43
561 查看
1.创建一个对象City.
2.序列化对象与反序列化对象
3.运行结果
JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JSONDemo { public class City { public string Country { get; set; } public IList<string> Name { get; set; } public City() { this.Name = new List<string> { "Auckland", "Wellington", "Hamilton" }; } } }
2.序列化对象与反序列化对象
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using GongHuiNewtonsoft.Json; using GongHuiNewtonsoft.Json.Serialization; using GongHuiNewtonsoft.Json.Converters; namespace JSONDemo { class Program { static void Main(string[] args) { JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() }; City city = new City(); string json = JsonConvert.SerializeObject(city); Console.WriteLine(json); City city1 = JsonConvert.DeserializeObject<City>(json); Console.WriteLine(city1.Country); Console.WriteLine(city1.Name[0]); Console.WriteLine(city1.Name[1]); Console.WriteLine(city1.Name[2]); } } }
3.运行结果
JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
相关文章推荐
- javascript中new关键字详解
- JS面向对象
- js里规定输入的数值只能是数字
- JS中Json对象和字符串之间的转换
- JSON使用DataContract和DataMember
- 浅谈Ajax
- javascript script load related【转】
- JSON 的标准:双引号而非单引号!
- 一天JavaScript示例-在功能上的标量参数和数组参数的差异
- JSP自定义标签开发入门
- 用js写图片轮播
- js----window对象
- JSON使用OnSerializing,OnSerialized,OnDeserializing,OnDeserialized
- js函数的作用域与this指向
- js的几个库
- JS核心系列:理解 new 的运行机制
- JS图片时间
- js实现拖拽效果(构造函数)
- 通过javascript在网页端生成zip压缩包并下载
- 从jsp向servlet传送数据的两种方式