JSON使用ObjectCreationHandling 反序列化替换不重复的值

来源:互联网 发布:苹果接电话变声软件 编辑:程序博客网 时间:2024/05/20 09:27

1.首先创建一个实例对象City,并声明构造函数.

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.JSON反序列化数据

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)        {            string json = @"{                'Country':'New Zealand',                'Name':[                    'Auckland',                    'Christchurch',                    'Dunedin'                ]            }";            Console.WriteLine("---------------替换前----------------");            City city1 = JsonConvert.DeserializeObject<City>(json);            foreach (string name in city1.Name)            {                Console.WriteLine(name);            }            Console.WriteLine("-------------替换后-------------------");            City city2 = JsonConvert.DeserializeObject<City>(json, new JsonSerializerSettings            {                ObjectCreationHandling = ObjectCreationHandling.Replace            });            foreach (string name in city2.Name)            {                Console.WriteLine(name);            }        }    }}


3.运行的结果

 

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

0 0
原创粉丝点击