我的json响应应该是这样的
结果:[{apps:[{PROD_appsr12:[{ora:[]},{logs:[]},{temp:[]},{},{},{},{},{}, {pids:[]},{admin:[]},{portal:[]},{appl:[]},{rgf:[]}]}]}]}
如何动态解析JSONArray
以上(如文件夹结构)。
这是我的代码`类Getdata扩展了AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Inst_Activity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
if (pref.getString("instraw", null) != null) {
Log.e("Enter Not Null>>>", "Not Null");
spString = pref.getString("instraw", null);
jsonRes = spString;
getServerData();
} else {
Log.e("Enter Null>>>", "Yes It's Null");
ServiceHandler handler = new ServiceHandler();
jsonRes = handler.makeServiceCall(url, ServiceHandler.GET);
editor.putString("instraw", jsonRes);
editor.commit();
getServerData();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
listView.setAdapter(new ArrayAdapter<>(Inst_Activity.this,
R.layout.schema_item, R.id.textView1, list1));
}
}
private void getServerData() {
Log.e("ENTER-getServerData>>>>", "0000000");
if (jsonRes != null) {
try {
mainObj = new JSONObject(jsonRes);
Iterator<String> keys = mainObj.keys();
while (keys.hasNext()) {
String key = keys.next();
Log.e("Main-key>>>>", key);
methFirst(key);
}
} catch (Exception e) {
}
} else {
}
}
private void methFirst(String list2) throws JSONException {
try {
Log.e("ENTER-methFirst>>>>", "1111111");
jsonArray = mainObj.getJSONArray(list2);
Log.i("MAIN-jsonArray", "" + jsonArray);
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject Obj = jsonArray.getJSONObject(j);
Log.i("MAIN-JSONObject Obj>>>>", "" + Obj);
getObjMeth(Obj);
}
} catch (Exception e) {
// TODO: handle exception
}
}
private void getObjMeth(JSONObject subObj2) {
Log.e("ENTER-methFirst-1>>>>", "333333333333");
Log.e("ENTER-getObjMeth>>>>", "entered");
try {
Iterator<String> i_keys = subObj2.keys();
while (i_keys.hasNext()) {
String strkey = i_keys.next();
Log.e("getObjMeth-key>>>>", strkey);
list1.add(strkey);
Log.e("<<keys-list1>>", "" + list1);
sub_sub_Obj = new JSONObject();
sub_sub_Obj = subObj2;
listView.setAdapter(new ArrayAdapter<>(Inst_Activity.this,
R.layout.schema_item, R.id.textView1, list1));
}
} catch (Exception e) {
// TODO: handle exception
}
}
private void methFirst1(String list3) {
Log.e("ENTER-methFirst-1>>>>", "22222222222");
Log.e("ENTER-methFirst1", "" + list3);
// Log.e("sub_sub_Obj-methFirst1", "" + sub_sub_Obj);
list1.clear();
try {
// for (int i = 0; i < list1.size(); i++) {
// if (list3 == list1.get(i)) {
if (list3 == mainObj.keys().toString()) {
Log.e("<<<matched", "yesssss");
} else {
Log.e("waste of time>>>>", "do");
}
subArray = sub_sub_Obj.getJSONArray(list3);
Log.e("size>>>>>>>>>>>", "" + subArray.length());
for (int j = 0; j < subArray.length(); j++) {
Log.i("Test----->>>>>>", "test--" + j);
JSONObject Obj = subArray.getJSONObject(j);
getObjMeth(Obj);
//break;
}
// }
// }
} catch (Exception e) {
}
}
` 请帮助我
答案 0 :(得分:0)
如果要迭代JSONArray,可以使用for循环执行:
for (int i = 0; i < myArray.length; i++) {
JSONObject myObject = (JSONObject) myArray.get(i);
String myString = myObject.getString("myString");
}