2

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

2
  • sorry your question is not clear, you want to get jsonarry for json array? Commented Sep 17, 2019 at 6:58
  • amm i just want get above response in my listview. Commented Sep 17, 2019 at 7:02

2 Answers 2

1

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();
    }
Sign up to request clarification or add additional context in comments.

4 Comments

let me check.:)
I have updated the code...Use "contains" in place of the "equalsIgnoreCase"
one more question bro why link i mentioned everytime download response file instead of showing me on browser.in postman its working fine
I have updated the solution, now it will work for you, Have look on it i use the "getString" instead of the "get"..Let me know in case of concern..
1

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();
        }

2 Comments

for checking JSON contains particular jsonArray key, you can use "has" and for you json data you can try try { // jsonString is a string variable that holds the JSON JSONArray itemArray=new JSONArray(jsonString); for (int i = 0; i < itemArray.length(); i++) { int value=itemArray.getInt(i); Log.e("json", i+"="+value); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Right, for the key exist or not you can use has or for compare you can use instanceof..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.