1, first need to install Org.json.jar
2. Class Jsonobject is used to create a JSON object. where Jsonobject.put (KEY, VALUE) is used to add an entry to it
3, Jsonobject.getstring (key) is used to get the value corresponding to the KEY from the JSON object, and the value is of type string
4, sometimes, value is a sub-JSON object, at this time we can use the Jsonobject.get (key) method, from which the key corresponding to the sub-JSON object, and its type into Jsonobject
5. Repeat the above operation to get the inline value field you want to get recursively
code example:
Import Org.json.jsonobject;public class Jsontest {public static void Main (string[] args) { Jsonobject json = new Jsonobject (); Json.put ("name", "Monster"); Jsonobject education = new Jsonobject (); Education.put ("Highschool", "XSHS"); Education.put ("university", "HUST"); Json.put ("Education", education); Jsonobject hobby = new Jsonobject (); Json.put ("hobby", hobby); SYSTEM.OUT.PRINTLN (JSON); String name = json.getstring ("name"); Jsonobject object = (jsonobject) json.get ("Education"); String University = object.getstring ("university"); System.out.println ("\" "+ name +" \ "finished University at \" "+ University +" \ "");} }
Output:
{"Education": {"University": "HUST", "Highschool": "Xshs"}, "name": "Monster", "hobby": {}} "monster" finished University At "HUST"
Java nested parsing JSON