Json-lib---> Getting Started

Source: Internet
Author: User

jsonobject This object can be seen as a map

Two methods of construction:

Jsonobject Jo = new Jsonobject ();

jsonobject Jo = Jsonobject.fromobject ( requires a Map object or JavaBean object here ) ;

Convert to JSON string: string json = jo.tostring ();

Jsonarray This object can be seen as a list

Two methods of construction:

Jsonarray ja = jsonarray.fromobject ();

Jsonarray ja = jsonarray.fromobject ( A Collection collection object is required here);

Convert to JSON string: string json = ja.tostring ();

Case Analysis:

---> Entity class Person.java

Requirements: ① must be public modified ② has getter and setter methods

1  PackageJSON;2 3  Public classPerson {4     PrivateString name;5     Private intAge ;6     PrivateString sex;7     8      PublicString GetName () {9         returnname;Ten     } One      Public voidsetName (String name) { A          This. Name =name; -     } -      Public intGetage () { the         returnAge ; -     } -      Public voidSetage (intAge ) { -          This. Age =Age ; +     } -      PublicString Getsex () { +         returnsex; A     } at      Public voidsetsex (String sex) { -          This. Sex =sex; -     } -      PublicPerson () {} -      PublicPerson (String name,intAge , String Sex) { -         Super(); in          This. Name =name; -          This. Age =Age ; to          This. Sex =sex; +     } -  the}

---> Test class Testjson.java

1  PackageJSON;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 8 ImportNet.sf.json.JSONArray;9 ImportNet.sf.json.JSONObject;Ten  One Importorg.junit.Test; A  -  Public classTestjson { -  the @Test -      Public voidFun () { -Jsonobject Jo =NewJsonobject ();//Create a Map object -Jo.put ("name", "Zhang San"); +Jo.put ("Age", 23); -Jo.put ("Sex", "male"); +String JSON =jo.tostring (); A         //{"name": "Zhang San", "age": +, "sex": "Male"} at System.out.println (JSON);  -     } - @Test -      Public voidfun1 () { -person P1 =NewPerson ("John Doe", "male"); -person P2 =NewPerson ("Harry", "female"); inmap<string,person> map =NewHashmap<string,person>(); -Map.put ("1", p1); toMap.put ("2", p2); +          -Jsonobject Mapjson = jsonobject.fromobject (map);//Create Mapjson the          * //{"2": {"age": +, "name": "Harry", "Sex": "Female"}, "1": {"Age": $, "name": "John Doe", "Sex": "Male"}} $ System.out.println (mapjson.tostring ());Panax Notoginseng          -          thePerson person =NewPerson ("LiSi", "female"); +Jsonobject Beanjson = jsonobject.fromobject (person);//Create Beanjson A          the         //{"Age": +, "name": "LiSi", "Sex": "Female"} + System.out.println (beanjson.tostring ()); -          $     } $      - @Test -      Public voidfun2 () { theperson P1 =NewPerson ("John Doe", "male"); -person P2 =NewPerson ("Harry", "female");Wuyilist<person> list =NewArraylist<person>(); the List.add (p1); - List.add (p2); WuJsonarray ja = jsonarray.fromobject (list);//To create an object from the list -String JSON =ja.tostring (); About //[{"Age": "" Name ":" John Doe "," Sex ":" Male "},{" age ":" Name ":" Harry "," Sex ":" Female "}] $ System.out.println (JSON);  -          -Jsonarray jsonlist =NewJsonarray (); - Jsonlist.add (p1); A Jsonlist.add (p2); + //[{"Age": "" Name ":" John Doe "," Sex ":" Male "},{" age ":" Name ":" Harry "," Sex ":" Female "}] the System.out.println (jsonlist.tostring ()); -     } $  the}

Json-lib---> Getting Started

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.