Android reads json data (traversing JSONObject and JSONArray)
Public String getJson (){
String jsonString = "{\" FLAG \ ": \" flag \ ", \" MESSAGE \ ": \" SUCCESS \ ", \" name \": [{\ "name \": \ "jack \" },{ \ "name \": \ "lucy \"}]} "; // json string
Try {
JSONObject result = new JSONObject (jsonstring); // convert to JSONObject
Int num = result. length ();
JSONArray nameList = result. getJSONArray ("name"); // get JSONArray
Int length = nameList. length ();
String aa = "";
For (int I = 0; I <length; I ++) {// traverse JSONArray
Log. d ("debugTest", Integer. toString (I ));
JSONObject oj = nameList. getJSONObject (I );
Aa = aa + oj. getString ("name") + "| ";
}
Iterator <?> It = result. keys ();
String aa2 = "";
String bb2 = null;
While (it. hasNext () {// traverse JSONObject
Bb2 = (String) it. next (). toString ();
Aa2 = aa2 + result. getString (bb2 );
}
Return aa;
} Catch (JSONException e ){
Throw new RuntimeException (e );
}
}