Serializing and deserializing generic data
Staticvoid Jsontest () {Jsondata JD =New Jsondata (); jd["Result"] =1; jd["User"] =New Jsondata ();* * When adding a new layer of relationships, you need to new** Jsondata again () 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);
Run results
{"Result": 1, "user": {"name": "Zhang", "Password": 123456}}
Result=1
Name=zhang
psw=123456
Serializing and deserializing data objects
Creating objects
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; } }
Operation
static void JsonTest2 () {person P1 = new person () .name = "Zhangsan" ; P1.age = 23; string JsonData = Jsonmapper "json=" + jsondata) ; Person p = Jsonmapper "name=" + p.name + " Age= "+ p.age" ;}
Run results
json={"name": "Zhangsan", "age": All, "adress": null}
Name=zhangsan age=23
Save object and object contents as key-value pairs
static void JsonTest3 () {person P1 = new person (); P1. Name ="Zhangsan"; P1. Age =23; Serializes an object to a string jsondata UserData = Jsonmapper. Toobject (Jsonmapper. ToJson (p1)); Jsondata JD = New Jsondata (); jd["user1"] = new Jsondata (); jd["User1"]. ADD (UserData);//Save the data in an array format jd["User2"] = new Jsondata (); jd["User2"] = UserData;//Save data in object format string jsondata = Jsonmapper. ToJson (JD); Console. WriteLine ("json=" + jsondata); Deserializes JSON data into objects jsondata JSON = Jsonmapper. Toobject (Jsondata); Person p = Jsonmapper "user2"]; Console "name=" + p.name + " Age= "+ p.age" ; Person pp = Jsonmapper "user1"][0]; Console "name=" + pp.name + "age=" + pp.age) ;
Run Result: User1 is saved as an array, User2 is saved as an object
json={"user1": [{"Name": "Zhangsan", "Age": $, "adress": null}],
"User2": {"name": "Zhangsan", "Age": $, "adress": null}}
Name=zhangsan age=23
Name=zhangsan age=23
Serialization and deserialization of collection data
Publicstatic 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 Span class= "Hljs-keyword" >list<person> (); list. Add (p1); list. ADD (p2); //generate JSON data string Listjson = Jsonmapper.tojson (list); Console.WriteLine (Listjson); Console.WriteLine ( "------------------------"); //parsing json list<person> personjsonlist = Jsonmapper.toobject<list<person>> (Listjson); for (int i = 0; i < Personjsonlist.count; i++) { Console.WriteLine ( "name" + Personjsonlist[i].name);}
Run results
[{"Name": "Zhangsan", "Age": $, "adress": {"province": "Henan", "City": "Luoyang"}},
{"Name": "Lisi", "age": $, "adress": {"province": "Hebei", "City": "Handan"}}]
Namezhangsan
Namelisi
http://blog.csdn.net/u011484013/article/details/51303674
How to use C # Litjson