How to use C # Litjson

Source: Internet
Author: User
Tags tojson

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.