1. Use standard JSON objects, such as org.json.JSONObject json = new Org.json.JSONObject (yourjsonstring), and then parse one by one by Get (keystring)
2. Construct a class MyObject, according to your question, which adds String name,list<myobject> child, two attributes, add Getter Setter method, Parsing is done through Com.google.gson.Gson. The steps are as follows:
String jsonstr = "{' name ': ' 111 ', ' child ': [{' Name ': ' 222 ', ' child ': ' {' name ': ' 333 '}]},{' name ': ' 2221 '}]}";
Gson Gson = new Gson ();
MyObject obj = Gson.fromjson (jsonstr,myobject.class);
1 Importjava.util.List;2 3 ImportCom.google.gson.Gson;4 5 Public classMyObject {6 String name;7List<myobject>Child ;8 PublicString GetName () {9 returnname;Ten } One Public voidsetName (String name) { A This. Name =name; - } - PublicList<myobject>Getchild () { the returnChild ; - } - Public voidSetChild (list<myobject>Child ) { - This. Child =Child ; + } - + Public Static voidMain (string[] args) { AString jsonstr = "{' name ': ' 111 ', ' child ': [{' Name ': ' 222 ', ' child ': ' {' name ': ' 333 '}]},{' name ': ' 2221 '}]}"; atGson Gson =NewGson (); -MyObject obj = Gson.fromjson (jsonstr,myobject.class); -System.out.println (Obj.getchild (). Get (0). GetName ());//--222 -System.out.println (Obj.getchild (). Get (1). GetName ());//--222 -System.out.println (Obj.getchild (). Get (0). Getchild (). Get (0). GetName ());//--333 - } in}
Quote from http://www.iteye.com/problems/88002 flashsnow, thank you very much.
How Java parses multiple layers of JSON data