简体   繁体   中英

How to get Jsonarray from Jsonarray if they dont have jsonobject in android

How to get jsonarray from jsonarray?

code which i have tried.

              for(loop) 
             List<String> stringList = new ArrayList<>();
                    try {
                        JSONArray responses = new JSONArray(response);
                        for (int i = 0; i < responses.length(); i++) {
                            stringList.add(responses.getString(i));
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

    i want to get this json:


         [
            "maj",
                [
                   "maja salvador",
                   "maje",
                   "majboos",
                   "major lazer",
                   "majili",
                   "majid al futtaim",
                   "majorca",
                   "majestic",
                   "major",
                   "majnoon qahwa"
                  ]
        ]

here is the link you can try. http://suggestqueries.google.com/complete/search?client=firefox&q=maj

You can try this one like below

try {
            JSONArray jsonArray = new JSONArray("YOUR_REPONSE");

            for (int i = 0; i < jsonArray.length(); i++) {

                if (jsonArray.get(i).toString().contains("[")) {
                    //json array

                    JSONArray innerJSSONArray = new JSONArray(jsonArray.getString(i));
                    for (int j = 0; j < innerJSSONArray.length(); j++) {

                        Log.e("JSONARRAY",innerJSSONArray.getString(i));

                    }


                }
                else
                {
                    Log.e("WITH_OUT_JSONARRAY",jsonArray.getString(i));

                }

            }


        }catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }

You can parse data using like bellow solution

JSONArray jsonArray;
        try {
            jsonArray = new JSONArray(jsonData); //Set your mixed data
            for (int i = 0; i < jsonArray.length(); i++) {
                Object object = jsonArray.get(i);
                if (object instanceof JSONArray) { // Check is it array or object 
                    JSONArray jsonSubArray = jsonArray.getJSONArray(i);
                    for (int k = 0; k < jsonSubArray.length(); k++) {
                        //Whatever you want to parse Key value object
                    }
                } else {
//                This is the JSON object so you can access directly key value
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM