Disclaimer: I've never heard of JSON before work, maybe it's too much for me. It is possible that Ajax in front of the foreground is exposed to JSON, and these days require a JSON string to be returned in pure Java programming.
There are two jar packages for parsing JSON objects on the Web: Json-lib.jar and Json.jar, which mainly introduce Json-lib.jar.
The jar package address is as follows:
Json-lib-2.4-jdk15.jar all the jar packages required. rar
One, Json-lib.jar also relies on the following jar packages:
- Commons-lang.jar
- Commons-beanutils.jar
- Commons-collections.jar
- Commons-logging.jar
- Ezmorph.jar
- Json-lib-2.2.2-jdk15.jar
Second, the application
JSON also exists in the form of Key-value. Key is a string, and value can be a basic type, Jsonarray, Jsonobject.
Jsonarray:[], words too literally also know that he is an array form, and you can put more than one JSON
jsonobject:{} just put a JSON.
Because they can nest in more forms.
Third, the output JSON instance, considering the comparison of [], {}, distinguish the duplicate variables, the name of the variable is capitalized, it appears to be nonstandard.
ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject; Public classJsontest { Public Static voidMain (string[] args) {jsonobject container1=NewJsonobject (); Container1.put ("ClassName", "31 class High"); System.out.println (Container1.tostring ()); Jsonarray ClassName=NewJsonarray (); Classname.add ("31 Class High"); Container1.put ("ClassName", ClassName); System.out.println (Container1.tostring ()); Jsonobject ClassInfo=NewJsonobject (); Classinfo.put ("Stucount", 50); Classinfo.put ("Leader", "RAH"); Container1.put ("ClassInfo", ClassInfo); System.out.println (Container1); Jsonobject ClassInfo=NewJsonobject (); Jsonarray Stucount=NewJsonarray (); Stucount.add (50); Jsonarray leader=NewJsonarray (); Leader.add ("Rah"); Classinfo.put ("Stucount", Stucount); Classinfo.put ("Leader", leader); Container1.put ("ClassInfo", ClassInfo); System.out.println (Container1); Jsonarray Students=NewJsonarray (); Jsonobject Studentone=NewJsonobject (); Studentone.put ("Name", "Mace"); Studentone.put ("Sex", "male"); Studentone.put ("Age", 12); Studentone.put ("Hobby", "Java Develop"); Jsonobject Studenttwo=NewJsonobject (); Studenttwo.put ("Name", "King Lame"); Studenttwo.put ("Sex", "male"); Studenttwo.put ("Age", 13); Studenttwo.put ("Hobby", "c/C + + develop"); Students.add (Studentone); Students.add (Studenttwo); Container1.put ("Students", students); System.out.println (Container1); Jsonarray Students=NewJsonarray (); Jsonobject Studnetone=NewJsonobject (); Jsonarray name1=NewJsonarray (); Name1.add ("Mace"); Jsonarray sex1=NewJsonarray (); Sex1.add (Male); Jsonarray Age1=NewJsonarray (); Age1.add ("12"); Jsonarray hobby1=NewJsonarray (); Hobby1.add ("Java Develop"); Studnetone.put ("Name", name1); Studnetone.put ("Sex", SEX1); Studnetone.put ("Age", Age1); Studnetone.put ("Hobby", hobby1); Jsonobject Studnettwo=NewJsonobject (); Jsonarray name2=NewJsonarray (); Name2.add ("The King Lame"); Jsonarray Sex2=NewJsonarray (); Sex2.add (Male); Jsonarray Age2=NewJsonarray (); Age2.add ("13"); Jsonarray hobby2=NewJsonarray (); Hobby2.add ("C-Develop"); Studnettwo.put ("Name", name2); Studnettwo.put ("Sex", SEX2); Studnettwo.put ("Age", Age2); Studnettwo.put ("Hobby", hobby2); Students.add (Studnetone); Students.add (Studnettwo); Container1.put ("Students", Students); System.out.println (Container1); Jsonarray Teachers=NewJsonarray (); Teachers.add (0, "Miss Wang"); Teachers.add (1, "Miss Li"); Container1.put ("Teachers", teachers); System.out.println (Container1); Jsonarray Teachers=NewJsonarray (); Jsonobject Teacher1=NewJsonobject (); Teacher1.put ("Name", "Xiao Mei"); Teacher1.put ("Introduce", "he's a good teacher."); Jsonobject Teacher2=NewJsonobject (); Teacher2.put ("Name", "Xiao Li"); Teacher2.put ("Introduce", "he's a qualified teacher."); Teachers.add (0, Teacher1); Teachers.add (1, Teacher2); Container1.put ("Teachers", Teachers); System.out.println (Container1); }}
Operation Result:
{"ClassName": "31 class High"}{"ClassName": "Gao 31 class", "ClassName": ["high 31 classes"]}{"ClassName": "Gao 31 class", "ClassName": ["high 31 classes"], "ClassInfo": {"Stucount": "Leader": "RAH"}}{"ClassName": "High Class 31", "ClassName": ["high 31 classes"], "ClassInfo": {"Stucount": "Leader", "" "" Rah "," ClassInfo ": {" Stucount ", [50 ], "leader": ["RAH"]}}{"ClassName": "High Class 31", "ClassName": ["high 31 classes"], "ClassInfo": {"Stucount": "Leader", "" "" Rah "," ClassInfo ": {" Stucount ", [50 ], "leader": ["rah"]}, "Students": [{"Name": "Mace", "Sex": "Male", "Age": "Hobby": "Java Develop"},{"name": "King Cripple", "Sex": "Male "," Age ":" Hobby ":" C-Develop "}]}{"ClassName": "High Class 31", "ClassName": ["high 31 classes"], "ClassInfo": {"Stucount": "Leader", "" "" Rah "," ClassInfo ": {" Stucount ", [50 ], "leader": ["rah"]}, "Students": [{"Name": "Mace", "Sex": "Male", "Age": "Hobby": "Java Develop"},{"name": "King Cripple", "Sex": "Male "," Age ":" Hobby ":" C + + Develop "}]," Students ": [{" Name ": [" Mace "]," sex ": [" male "]," age ": [" ["]," hobby ": [" Java Develop "]},{" name ": [" King Cripple "]," sex ": [" male "]," age ": ["], "" Hobby ": [" C + + Develop "]}]}{"ClassName": "High Class 31", "ClassName": ["high 31 classes"], "ClassInfo": {"Stucount": "Leader", "" "" Rah "," ClassInfo ": {" Stucount ", [50 ], "leader": ["rah"]}, "Students": [{"Name": "Mace", "Sex": "Male", "Age": "Hobby": "Java Develop"},{"name": "King Cripple", "Sex": "Male "," Age ":" Hobby ":" C + + Develop "}]," Students ": [{" Name ": [" Mace "]," sex ": [" male "]," age ": [" ["]," hobby ": [" Java Develop "]},{" name ": [" The King Lame "]," sex ": [" male "]," age ": [" "]," hobby ": [" C + + Develop "]}]," Teachers ": [" Miss Wang "," Miss Li "]}{"ClassName": "High Class 31", "ClassName": ["high 31 classes"], "ClassInfo": {"Stucount": "Leader", "" "" Rah "," ClassInfo ": {" Stucount ", [50 ], "leader": ["rah"]}, "Students": [{"Name": "Mace", "Sex": "Male", "Age": "Hobby": "Java Develop"},{"name": "King Cripple", "Sex": "Male "," Age ":" Hobby ":" C + + Develop "}]," Students ": [{" Name ": [" Mace "]," sex ": [" male "]," age ": [" ["]," hobby ": [" Java Develop "]},{" name ": [" King Lame "]," sex ": [" male "]," age ": [" ["]," hobby ": [" C + + Develop "]}]," Teachers ": [" Miss Wang "," Miss Li "]," teachers ": [{ "Name": "Xiao Mei", "introduce": "He is a good teacher"},{"name": "Xiao Li", "introduce": "He is a qualified teacher"}]}
Iv. iterating through the JSON instance
The JSON string for the above output is sequentially traversed
String ClassName1 = (string) container1.get ("ClassName"); System.out.println ("ClassName data is:" +ClassName1); Jsonarray className1= Container1.getjsonarray ("ClassName"); System.out.println ("ClassName data is:" +className1); Jsonobject ClassInfo1= Container1.getjsonobject ("ClassInfo"); System.out.println ("ClassInfo data is:" +classInfo1); Jsonobject ClassInfo1= Container1.getjsonobject ("ClassInfo"); System.out.println ("ClassInfo data is:" +ClassInfo1); Jsonarray Students1= Container1.getjsonarray ("Students"); System.out.println ("Students data is:" +students1); Jsonarray Students1= Container1.getjsonarray ("Students"); System.out.println ("Students data is:" +Students1); Jsonarray teachers1= Container1.getjsonarray ("Teachers"); for(inti=0; I < teachers1.size (); i++) {System.out.println ("Teahcer" + i + "is:" +Teachers1.get (i)); } Jsonarray Teachers1= Container1.getjsonarray ("Teachers"); for(inti=0; I < teachers1.size (); i++) {System.out.println ("Teachers" + i + "is:" +Teachers1.get (i)); }
Traversal results:
ClassName data is: High 31 class ClassName data is: ["high Class 31"]classinfo data is: {"Stucount": "Leader": "RAH" }classinfo data is: {"stucount": [[A], "leader": ["RAH"]}students data is: [{"name": "Mace", "Sex": "Male", " Age ":" Hobby ":" Java Develop "},{" name ":" King Lame "," sex ":" Male "," Age ":" Hobby ":" C + + develop "}]students data is: [{ "Name": ["Mace"], "sex": ["male"], "age": ["["], "hobby": ["Java Develop"]},{"name": ["King Cripple"], "sex": ["male"], "age": ["13"], " Hobby ": [" C/S++Develop "0 1 0 is: {" name ":" Xiao Mei "," introduce ":" Heis a good teacher "1 is: {" name ": "Xiao Li", "introduce": "He is a qualified teacher"}
The above includes most of the nested forms of JSON, may be ignored can also refer to the above content.
The use of JSON objects in Java