C# litJson 使用方法

对一般数据进行序列化和反序列化操作


 static void jsonTest() {
            //
            JsonData jd = new JsonData();
            jd["result"] = 1;
            jd["user"] = new JsonData();//**新添加一层关系时,需要再次 new** JsonData()
            jd["user"]["name"] = "zhang";
            jd["user"]["password"] = 123456;
            string jsonData = JsonMapper.ToJson(jd);
            Console.WriteLine(jsonData);
            //
            JsonData deJson=JsonMapper.ToObject(jsonData);
            int result = int.Parse(deJson["result"].ToJson());
            string name = deJson["user"]["name"].ToString();
            int psw = int.Parse(deJson["user"]["password"].ToString());
            Console.WriteLine( "result="+result);
            Console.WriteLine("name="+name);
            Console.WriteLine("psw="+psw);
        }

运行结果 
{“result”:1,”user”:{“name”:”zhang”,”password”:123456}} 
result=1 
name=zhang 
psw=123456

对数据对象进行序列化和反序列化操作


创建对象

  class Adress
    {
        public string province { set; get; }
        public string city { set; get; }
    }
    class Person
    {
        public string name { set; get; }
        public int age { set; get; }
        public Adress adress { set; get; }
    }

操作

static void jsonTest2()
        {
            Person p1 = new Person();
            p1.name = "zhangsan";
            p1.age = 23;

            string jsonData = JsonMapper.ToJson(p1);
            Console.WriteLine("json=" + jsonData);


            Person p = JsonMapper.ToObject<Person>(jsonData);
            Console.WriteLine("name=" + p.name + "  age=" + p.age);

        }

运行结果 
json={“name”:”zhangsan”,”age”:23,”adress”:null} 
name=zhangsan age=23

将对象和对象内容以键值对的形式进行保存


 static void jsonTest3()
        {
            Person p1 = new Person();
            p1.name = "zhangsan";
            p1.age = 23;
            //将对象序列化为字符串
            JsonData userData = JsonMapper.ToObject(JsonMapper.ToJson(p1));
            JsonData jd = new JsonData();
            jd["user1"] = new JsonData();
            jd["user1"].Add(userData);//以数组的格式保存数据

            jd["user2"] = new JsonData();
            jd["user2"] = userData;//以对象的格式保存数据
            string jsonData = JsonMapper.ToJson(jd);
            Console.WriteLine("json=" + jsonData);

            //把json数据反序列化为对象
            JsonData json = JsonMapper.ToObject(jsonData);
            Person p = JsonMapper.ToObject<Person>(json["user2"].ToJson());
            Console.WriteLine("name=" + p.name + "  age=" + p.age);

            Person pp = JsonMapper.ToObject<Person>(json["user1"][0].ToJson());
            Console.WriteLine("name=" + pp.name + "  age=" + pp.age);

        }

运行结果:user1以数组的形式保存,user2以对象的形式保存 
json={“user1”:[{“name”:”zhangsan”,”age”:23,”adress”:null}], 
“user2”:{“name”:”zhangsan”,”age”:23,”adress”:null}} 
name=zhangsan age=23 
name=zhangsan age=23

将集合数据进行序列化和反序列化


 public static void jsonTest4()
        {
            Person p1 = new Person();
            p1.name = "zhangsan";
            p1.age = 23;
            Adress adress1 = new Adress();
            adress1.province = "HeNan";
            adress1.city = "LuoYang";
            p1.adress = adress1;

            Person p2 = new Person();
            p2.name = "lisi";
            p2.age = 20;
            Adress adress2 = new Adress();
            adress2.province = "HeBei";
            adress2.city = "HanDan";
            p2.adress = adress2;

            List<Person> list = new List<Person>();
            list.Add(p1);
            list.Add(p2);

            //生成json数据
            string listJson = JsonMapper.ToJson(list);
            Console.WriteLine(listJson);
            Console.WriteLine("------------------------");
            //解析json
            List<Person> personJsonList = JsonMapper.ToObject<List<Person>>(listJson);
            for (int i = 0; i < personJsonList.Count; i++)
            {
                Console.WriteLine("name" + personJsonList[i].name);
            }

        }

运行结果 
[{“name”:”zhangsan”,”age”:23,”adress”:{“province”:”HeNan”,”city”:”LuoYang”}}, 
{“name”:”lisi”,”age”:20,”adress”:{“province”:”HeBei”,”city”:”HanDan”}}]


namezhangsan 
namelisi

http://blog.csdn.net/u011484013/article/details/51303674

posted @ 2018-02-03 21:11  核电站  阅读(356)  评论(0编辑  收藏  举报