Kriti Jain Kriti Jain - 3 months ago 17
Android Question

How to split value from json String?

I want to split values of json String into multiple string. I am getting values in "images" thats is (image1, image2, image3) i want to split this string into 3 different strings, I am not getting how i acheive this

protected void status() {

try {


JSONArray property_images = new JSONArray(myJSON);

for(int i=1;i<property_images.length();i++){

JSONObject c = property_images.getJSONObject(i);
img = c.getString("images");

}


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


This is Json Format

[{"img_id":"6","listing_id":"1","images":"images (3).jpg","slider_status":"yes","date":"0000-00-00"},{"img_id":"7","listing_id":"1","images":"images (4).jpg","slider_status":"yes","date":"0000-00-00"},{"img_id":"8","listing_id":"1","images":"474harvester2008C-525x328.jpg","slider_status":"yes","date":"0000-00-00"}]

Answer

Your loop starts with 1 instead of 0 btw. Other than that you can use a String array to store the image fields.

String[] stringArray = new String[property_images.length()];

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

JSONObject c = property_images.getJSONObject(i);
stringArray[i] = c.getString("images");

}

for (String s: stringArray)
    System.out.println(s);

With your given json, the output is this:

"images (3).jpg"  
"images (4).jpg"
"474harvester2008C-525x328.jpg"
Comments