如何在android中解析这种类型的json数组?

时间:2017-09-21 08:40:09

标签: android android-json

我知道在单个数组中解析JSON数组但是如何传递多个JSON数组并将其值设置为require enter image description here

 {"scode":"200","all_menu":[{"app_menu_id":"67","app_menu_name":"Demograpics","all_sub_menu":[{"app_menu_id":"67","app_sub_menu_id":"47","app_sub_menu_name":"\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/8451504072003.jpg"}],"sub_menu":"true"},{"app_menu_id":"68","app_menu_name":"Lyrics","all_sub_menu":[{"app_menu_id":"68","app_sub_menu_id":"48","app_sub_menu_name":"Music","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4681504072092.jpg"}],"sub_menu":"true"},{"app_menu_id":"69","app_menu_name":"Adult","all_sub_menu":[{"app_menu_id":"69","app_sub_menu_id":"49","app_sub_menu_name":"Double
 Meaning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/9931504072151.jpg"}],"sub_menu":"true"},{"app_menu_id":"70","app_menu_name":"Emotions","all_sub_menu":[{"app_menu_id":"70","app_sub_menu_id":"50","app_sub_menu_name":"Love","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/7611504072164.jpg"}],"sub_menu":"true"},{"app_menu_id":"71","app_menu_name":"Wishes","all_sub_menu":[{"app_menu_id":"71","app_sub_menu_id":"51","app_sub_menu_name":"Good
 Morning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/5171504072183.jpg"}],"sub_menu":"true"},{"app_menu_id":"72","app_menu_name":"Among Friend","all_sub_menu":[{"app_menu_id":"72","app_sub_menu_id":"52","app_sub_menu_name":"Friendship","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4411504072205.jpg"}],"sub_menu":"true"},{"app_menu_id":"73","app_menu_name":"Jokes","all_sub_menu":[{"app_menu_id":"73","app_sub_menu_id":"53","app_sub_menu_name":"Santa
 Banta","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4331504072225.jpg"}],"sub_menu":"true"},{"app_menu_id":"74","app_menu_name":"Featured","all_sub_menu":[{"app_menu_id":"74","app_sub_menu_id":"54","app_sub_menu_name":"Ganpati
 Bappa","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4771504072247.jpg"}],"sub_menu":"true"}]}

2 个答案:

答案 0 :(得分:1)

假设“响应”是您的JSONResponse

JSONObject jsonObject = new JSONObject(response);// This is used to get jsonObject from response

String sCode=jsonObject.optString("scode"); // This is how you can parse string from jsonObject
JSONArray allmenuArray=jsonObject.optJSONArray("all_menu"); //This is how you can parse JsonArray from jsonObject
for(int i=0;i<allmenuArray.length();i++){
JSONObject objectJson=allmenuArray.optJSONObject(i);//This is how you can parse jsonObject from jsonArray
}

喜欢这个你可以解析你所有的jsonObject和jsonarray。只需按照以下步骤操作即可轻松解析完整的JSONResponse

答案 1 :(得分:0)

试试这个。

try {
    JSONObject jsonObject = new JSONObject("JSONResponse");

    String scode = jsonObject.optString("scode");
    JSONArray allmenuArray = jsonObject.optJSONArray("all_menu");

    for (int i = 0; i < allmenuArray.length(); i++) {
        JSONObject objectJson = allmenuArray.optJSONObject(i);
        boolean sub_menu = objectJson.getBoolean("sub_menu");
        String app_menu_id = objectJson.getString("app_menu_id");
        String app_menu_name = objectJson.getString("app_menu_name");
        JSONArray all_sub_menu = objectJson.getJSONArray("all_sub_menu");

        for (int j = 0; j < all_sub_menu.length(); j++) {
            JSONObject data = allmenuArray.optJSONObject(j);
            Log.e("app_menu_id", data.getString("app_menu_id"));
            Log.e("app_sub_menu_id", data.getString("app_sub_menu_id"));
            Log.e("app_sub_menu_name", data.getString("app_sub_menu_name"));
            Log.e("app_sub_menu_image", data.getString("app_sub_menu_image"));
        }
    }
} catch (JSONException e) {
    Log.e("ERROr", e.toString());
}