For example, the following format JSON:
Create the corresponding Java object, and note that the inner class is defined as a static
Public class HResult { public String total; Public String Records; Public String page; Public list rows; Static class hcell{ public list<string> cell; Public String ID; } }
Using Gson for parsing
Public Static voidMain (string[] args) {String json= "{\" total\ ": \" 1\ ", \" Rows\ ": [{\" cell\ ": [\" c1\ ", \" c2\ "," \ "did not retrieve relevant historical information, please click [I want Message]\", \ "c4\", \ "C5\", \ "c6\", \ "C7\"],\ "id\": \ "999999999999999999999999999999\"}],\ "records\": \ "0\", \ "page\": \ "1\"} "; Gson Gson=NewGson (); HRESULT HRESULT= Gson.fromjson (JSON,NewTypetoken() {}.gettype ()); System.out.println ("Total:" +hresult.total); System.out.println ("Records:" +hresult.records); System.out.println ("Page:" +hresult.page); List<HResult.HCell> list =hresult.rows; for(Hresult.hcell row:list) {System.out.println ("Cell:" +Row.cell); System.out.println ("ID:" +row.id); } }
Operation Result:
run:total: 1 Records: 0 page: 1 cell:[c1, C2, did not retrieve the relevant historical information, please click [I want to leave a message], C4, C5, C6, C7] ID:9999999999999999999999999999990 seconds)
Gson Parsing complex JSON objects