-2

I have a JSON response in this format:

{
    "success": true,
    "categories": [{
        "id": "774",
        "name": "1"
    }, {
        "id": "774",
        "name": "1"
    }]
}

And I am parsing it like this:

try {
 JSONObject obj = new JSONObject(response);
 String success =  String.valueOf(obj.getBoolean("success"));

 JSONArray arr =  obj.getJSONArray("categories");
 //loop through each object
 for (int i=0; i<arr.length(); i++) {

   JSONObject jsonProductObject = arr.getJSONObject(i);
   String name = jsonProductObject.getString("name");
   String url = jsonProductObject.getString("id");
   Toast.makeText(getApplicationContext(),name, Toast.LENGTH_LONG).show();
 }
} catch (JSONException e) {
   e.printStackTrace();
}

But I only get the value of success. What I'm doing wrong here?

4
  • you are fetching wrong array. It should be JSONArray arr = obj.getJSONArray("categories"); instead of JSONArray arr = obj.getJSONArray("checkouts"); Commented Jan 8, 2016 at 6:41
  • post your logcat errors Commented Jan 8, 2016 at 6:48
  • 1
    why you are getting success value in a boolean like this obj.getBoolean("success") its wrong way as json is comming in the form of string fetch its value like this String success = obj.getString("success"); Commented Jan 8, 2016 at 6:49
  • The key of success is boolean, so i did just that. Commented Jan 10, 2016 at 7:43

3 Answers 3

1

Parse as below -

JSONObject obj = new JSONObject(json);

String success =  obj.getString("success");
JSONArray arr =  obj.getJSONArray("categories");
//loop through each object
for (int i=0; i<arr.length(); i++) {

    JSONObject jsonProductObject = arr.getJSONObject(i);
    String name = jsonProductObject.getString("name");
    String url = jsonProductObject.getString("id");

}
Sign up to request clarification or add additional context in comments.

1 Comment

I like the simplicity of the method. Thanks.
1

correct json key

JSONArray arr = obj.getJSONArray("checkouts");

replace by:

JSONArray arr = obj.getJSONArray("categories");

2 Comments

I edited the code, that was my mistake. I'm using categories key now but its the same result.
hey it should not be happen as i tested your code its all working..
1

DO like this,

if (!result.equalsIgnoreCase("")) {
                try {
                JSONObject _jsonObject = new JSONObject(result);
                boolean json = false;

                    json = _jsonObject.getBoolean("Status");
                    JSONArray jsonArray1 =  _jsonObject.getJSONArray("categories");

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

                    JSONObject jsonObject = jsonArray1.getJSONObject(i);
                    String name = jsonObject.getString("name");
                    String id = jsonObject.getString("id");

                }
                } catch (Exception e) {
                    Utils.printLoge(5, "error parse json", "--->" + e.getMessage());
                    return "ERROR";
                }
            }

2 Comments

is your peoblem solved ? @Steve Kamau
Yes it has thank you very much for your help!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.