天涯之外

导航

JSON.net的使用

using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System.Data;
using System.Data.SQLite;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Product product = new Product();
            product.Name = "[]{}\\''\"\"Apple";
            product.Expiry = new DateTime(2008, 12, 28);
            product.Price = 3.99M;
            product.Sizes = new string[] { "Small", "Medium", "Large" };           
            string output = JavaScriptConvert.SerializeObject(product);
            Console.WriteLine("Product:");
            Console.WriteLine(output);
            //{//  "Name": "Apple",//  "Expiry": new Date(1230422400000),//  "Price": 3.99,//  "Sizes": [//    "Small",//    "Medium",//    "Large"//  ]//}
            Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));
            string[] values = new string[] { "Small", "Medium", "Large" };
            output = JavaScriptConvert.SerializeObject(values);
            Console.WriteLine("strings :");
            Console.WriteLine(output);
           string[] deserializedString = (string[])JavaScriptConvert.DeserializeObject(output, typeof(string[]));
        }
    }
    public class Product
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        private DateTime _Expiry;

        public DateTime Expiry
        {
            get { return _Expiry; }
            set { _Expiry = value; }
        }

        private decimal _Price;

        public decimal Price
        {
            get { return _Price; }
            set { _Price = value; }
        }
        private string[] _Sizes;

        public string[] Sizes
        {
            get { return _Sizes; }
            set { _Sizes = value; }
        }
 
 
    }
}

json.net 可以从http://www.newtonsoft.com/products/json/下载

posted on 2009-05-24 15:49  天涯之外  阅读(456)  评论(1编辑  收藏  举报