1

It' easy to get a value from JSON when you have the string key, but what if you have situation like that:

{
  "images":["URL1"]
}

And array and no key inside? I use this code:

JSONArray imagesArr = propertiesjsonObject.getJSONArray("images");
for (int y=0; i<imagesArr.length(); y++)
{
  JSONObject imagesJsonObject = imagesArr.getJSONObject(y);
  String str_image_url = imagesJsonObject.get("HOW TO GET THE VALUES HERE?");
}

Propably it's ultra easy. Sorry to ask but I could not find proper example. PS. I use: import org.json.JSONArray; import org.json.JSONObject;

PS2: For now there is only one element in array, but in future I suppouse there might be more.

2 Answers 2

7

Try with this:

ArrayList<String> urls = new ArrayList<String>();
JSONArray imagesArr = propertiesjsonObject.getJSONArray("images");
for (int i = 0; i < imagesArr.length(); i++) {
    String str_image_url = imagesArr.getString(i);
    urls.add(str_image_url);
}

urls is an array with all the url you got

Hope this helps

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

1 Comment

Yes it's Ultra easy :) . Thanx. +1.
1
String str_image_url = imagesArr.getString(0);//you specify position in array here

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.