如何从json String中拆分值?

时间:2016-09-01 13:51:15

标签: android json android-json

我想将json String的值拆分为多个字符串。我正在获取“图像”中的值,即(image1,image2,image3)我想将此字符串拆分为3个不同的字符串,我不知道如何实现这一点

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

这是Json格式

[{"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"}]

1 个答案:

答案 0 :(得分:1)

您的循环以1而非0 btw开头。除此之外,您可以使用String数组来存储图像字段。

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

使用给定的json,输出为:

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