日期:2014-05-16  浏览次数:20411 次

Json.net 常用使用小结

3.5版本dll下载:http://pan.baidu.com/s/1c0vgNWc

using System;
using System.Linq;
using System.Collections.Generic;

namespace microstore
{
    public interface IPerson
    {
        string FirstName
        {
            get;
            set;
        }
        string LastName
        {
            get;
            set;
        }
        DateTime BirthDate
        {
            get;
            set;
        }
    }
    public class Employee : IPerson
    {
        public string FirstName
        {
            get;
            set;
        }
        public string LastName
        {
            get;
            set;
        }
        public DateTime BirthDate
        {
            get;
            set;
        }

        public string Department
        {
            get;
            set;
        }
        public string JobTitle
        {
            get;
            set;
        }
    }
    public class PersonConverter : Newtonsoft.Json.Converters.CustomCreationConverter<IPerson>
    {
        //重写abstract class CustomCreationConverter<T>的Create方法
        public override IPerson Create(Type objectType)
        {
            return new Employee();
        }
    }

    public partial class testjson : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //    TestJson();
        }

        #region 序列化
        public string TestJsonSerialize()
        {
            Product product = new Product();
            product.Name = "Apple";
            product.Expiry = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd hh:mm:ss");
            product.Price = 3.99M;
            //product.Sizes = new string[] { "Small", "Medium", "Large" };

            //string json = Newtonsoft.Json.JsonConvert.SerializeObject(product); //没有缩进输出
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(product, Newtonsoft.Json.Formatting.Indented);
            //string json = Newtonsoft.Json.JsonConvert.SerializeObject(
            //    product, 
            //    Newtonsoft.Json.Formatting.Indented,
            //    new Newtonsoft.Json.JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }
            //);
            return string.Format("<p>{0}</p>", json);
        }
        public string TestListJsonSerialize()
        {
            Product product = new Product();
            product.Name = "Apple";
            product.Expiry = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd hh:mm:ss");
            product.Price = 3.99M;
            product.Sizes = new string[] { "Small", "Medium", "Large" };

            List<Product> plist = new List<Product>();
            plist.Add(product);
            plist.Add(product);
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(plist, Newtonsoft.Json.Formatting.Indented);
            return string.Format("<p>{0}</p>", json);
        }
        #endregion

        #region 反序列化
        public string TestJsonDeserialize()
        {
            string strjson = "{\"Name\":\"Apple\",\"Expiry\":\"2014-05-03 10:20:59\",\"Price\":3.99,\"Sizes\":[\"Small\",\"Medium\",\"Large\"]}";
            Product p = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>(strjson);

            string template = @"<p><ul>
                                    <li>{0}</li>
                                    <li>{1}</li>
                                    <li>{2}</li>