C#应用Newtonsoft.Json操作json

Newtonsoft.Json是一个开源的C#操作json的项目,应用起来非常简单。其github地址;

下面的代码演示了如何应用Newtonsoft.Json序列号和反序列化。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace JsonNetDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Project> pList = new List<Project>();
            Project p = new Project();
            p.TestSystemList = new List<TestSystem>();
            p.entrustCellphone = "12";
            p.entrustName = "12";
            p.entrustUnit = "12";
            p.goodsModel = "12";
            p.pid = 34199;
            p.productionCellphone = "12";
            TestSystem t = new TestSystem();
            t.eid = 78611;
            t.name = "5.8G无线局域网(移动终端)";
            p.TestSystemList.Add(t);
            pList.Add(p);
            var json = JsonConvert.SerializeObject(pList);
            Console.WriteLine(json);
            Console.Read();
            string s = "[{\"productionCellphone\":\"12\",\"entrustCellphone\":\"12\",\"entrustUnit\":\"12\",\"pid\":34199,\"goodsModel\":\"12\",\"entrustName\":\"12\",\"TestSystemList\":[{\"eid\":78611,\"name\":\"5.8G无线局域网(移动终端)\"}]}]";
            var ret = JsonConvert.DeserializeObject<List<Project>>(s);
            Console.WriteLine(ret.Count);
            Console.Read();
        }
    }
    class Project
    {
        public string productionCellphone { get; set; }
        public string entrustCellphone { get; set; }
        public string entrustUnit { get; set; }
        public long pid { get; set; }
        public string goodsModel { get; set; }
        public string entrustName { get; set; }
        public List<TestSystem> TestSystemList { get; set; }
    }
    class TestSystem
    {
        public long eid { get; set; }
        public string name { get; set; }
    }
}
posted @ 2015-05-25 17:08  wardensky  阅读(506)  评论(0编辑  收藏  举报