1

My JSON structure is simple :

 "categories": [2],

I am succesfull in getting this output on my Android studio code as :

  JSONArray jsonArray = jsonObject.getJSONArray("categories");
  Log.e("CAT ID", String.valueOf(jsonArray));

Output is :

E/CAT ID: [2]  //and other IDS respectively

My question is how can I remove the braces and just get the Integer values?

2 Answers 2

3

You can call get() to get array value.

//If array contains only one value use get(0)

    JSONArray jsonArray = jsonObject.getJSONArray("categories");
    
    jsonArray.get(0);

//If Array contains more value use for() loop

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

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

Comments

0

jsut use below code :

String.valueOf(jsonArray)
    .replace("[","")
    .replace("]","")

or

jsonArray.toString()
    .replace("[","")
    .replace("]","")

Comments

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.