JSON使用IContractResolver序列化实现属性名骆驼命名法

来源:互联网 发布:淘宝哪家杂货铺好 编辑:程序博客网 时间:2024/05/22 15:30

1.先创建一个Address对象,属性采用首字母大写

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace JSONDemo{    public class Address    {        public string Province { get; set; }        public string City { get; set; }        public string County { get; set; }        public string DetailAddress        {           get{ return County + " " + City + " " + Province; }                    }    }}


2.下面是未使用和使用IContractResolver序列化

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using GongHuiNewtonsoft.Json;using GongHuiNewtonsoft.Json.Serialization;namespace JSONDemo{    class Program    {        static void Main(string[] args)        {            Address address = new Address            {                Province = "Hunan",                City = "Changde",                County = "Taoyuan"            };            string json = JsonConvert.SerializeObject(address, Formatting.Indented);            Console.WriteLine(json);            string json1 = JsonConvert.SerializeObject(address, Formatting.Indented, new JsonSerializerSettings            {                ContractResolver = new CamelCasePropertyNamesContractResolver()            });            Console.WriteLine(json1);        }    }}


3.运行后结果是

 

JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751

0 0