C# litJson 使用要领

时间:2021-12-01 04:59:10

对一般数据进行序列化和反序列化操纵 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); } }