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