简体   繁体   中英

iterate through an a json value containing an array to pick an image string

I am able to fetch values the db and pass it to a string array as shown

String[] strArrayCol = new String[6];
strArrayCol[4] = json_data.getString("images");

if you print the above you gets:

[{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/wendzj5atiks45c3zw00.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/rg04t5vcp4yxwdew677n.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/3yvy9f970vit2pxascv7.png"}]

my attempt is on performing something like

ArrayList<String[]> imgCol...
imgCol.add(strArrayCol );

 for (String [] val : imgCol){
                                System.out.println( val[4]);
                            }

let it prints

http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/wendzj5atiks45c3zw00.png
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/rg04t5vcp4yxwdew677n.png
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/3yvy9f970vit2pxascv7.png

please how can I achieve this

Based on your code and explanation, it seems you are JSON object "images" is the below string:

{images:[{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/wendzj5atiks45c3zw00.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/rg04t5vcp4yxwdew677n.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/3yvy9f970vit2pxascv7.png"}])}

Which is a JSONArray for the value of images.., Your code should be like this to retrieve the value of path

I have removed extra backward slashes in the string.

JsonImplement.parseJson("{\"images\":[{\"path\":\"http://10.0.2.2:88//web/uploads/images/yi6ej6f524bepyujh49y.png\"},{\"path\":\"http://10.0.2.2:88//web/uploads/images/wendzj5atiks45c3zw00.png\"},{\"path\":\"http://10.0.2.2:88//web/uploads/images/rg04t5vcp4yxwdew677n.png\"},{\"path\":\"http://10.0.2.2:88//web/uploads/images/3yvy9f970vit2pxascv7.png\"}]}");

And this is the logic I wrote in another method...

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
JSONArray arr = (JSONArray) jsonObject.get("images");
for(int i=0;i<arr.size();i++)
{
    JSONObject obj2 = (JSONObject) arr.get(i);
    System.out.println(obj2.get("path"));
}

This will give you output of image path's

http://10.0.2.2:88//web/uploads/images/yi6ej6f524bepyujh49y.png
http://10.0.2.2:88//web/uploads/images/wendzj5atiks45c3zw00.png
http://10.0.2.2:88//web/uploads/images/rg04t5vcp4yxwdew677n.png
http://10.0.2.2:88//web/uploads/images/3yvy9f970vit2pxascv7.png

If you know that your image file names will always be formatted as follows:

{path}/{filename}

Then there is a simple technique to find the file name.

  1. Find the last slash (/) character in the string.
  2. The filename is everything that follows the last slash character.

For example:

http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png

has a "path" value of "http://10.0.2.2:88//web/uploads/images/" and a "filename" value of "yi6ej6f524bepyujh49y.png"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM