1

I'm a little stuck with the following problem.

From a MongoDB I get this JSON string, and I want to get all the valuas of 'photo' and put them in an ArrayList. I've found some examples but they aren't working for me. I'm using GSON but a JSON solution will also be fine.

Return from MongoDB:

 [ { "url" : "/photos/avatar-1.jpg" , "photo" : "avatar-1.jpg" , "description" : "test 1"} , { "url" : "/photos/avatar-2.jpg" , "photo" : "avatar-2.jpg" , "description" : "test 2"} , { "url" : "/photos/avatar-3.jpg" , "photo" : "avatar-3.jpg" , "description" : "test 3"} , { "url" : "/photos/avatar-4.jpg" , "photo" : "avatar-4.jpg" , "description" : "test 4"}]

Putting into an ArrayList isn't the problem, but getting the 'photo' value. If someone could give me an example on how to loop through the 4 arrays and a System.out.println of the 'photo' value, that would be great!!

Thanks!

Wooble
90.6k12 gold badges111 silver badges132 bronze badges
asked Aug 17, 2012 at 13:28
0

3 Answers 3

2
jsonString =[...];
Gson gson = new Gson();
PhotoDTO[] photos = gson.fromJson(jsonString, PhotoDTO[].class);
for(PhotoDTO photo : photos){
 System.out.println("photo -> " + photo.getPhoto());
}

PhotoDTO Class Definition

class PhotoDTO
{
 String url;
 String photo;
 String description;
 // setters & getters methods
}
answered Aug 17, 2012 at 13:37
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This worked. The [] in PhotoDTO[] was what I was missing.
2

Using the excellent json-simple library, you should be able to do something like this:

String json = "[{\"photo\": \"1.png\"}, {\"photo\": \"2.png\"}, " +
 "{\"photo\": \"3.png\"}, {\"photo\": \"4.png\"}]";
JSONArray photos = (JSONArray)JSONValue.parse(json);
for (int index = 0; index < photos.size(); index++) {
 JSONObject photoObj = (JSONObject)photos.get(index);
 System.out.println(photoObj.get("photo"));
}

Obviously replace the literal JSON text with the data that you're fetching from your database.

answered Aug 17, 2012 at 13:37

Comments

0

Another option without creating DTOs, using Gson class StringMap:

String json = "[ { \"url\" : \"/photos/avatar-1.jpg\" , \"photo\" : \"avatar-1.jpg\" , \"description\" : \"test 1\"} , { \"url\" : \"/photos/avatar-2.jpg\" , \"photo\" : \"avatar-2.jpg\" , \"description\" : \"test 2\"} , { \"url\" : \"/photos/avatar-3.jpg\" , \"photo\" : \"avatar-3.jpg\" , \"description\" : \"test 3\"} , { \"url\" : \"/photos/avatar-4.jpg\" , \"photo\" : \"avatar-4.jpg\" , \"description\" : \"test 4\"}]"; 
Gson gson = new Gson(); 
Type type = new TypeToken<List<StringMap<String>>>(){}.getType();
List<StringMap<String>> stringMaps = gson.fromJson(json, type);
for (StringMap<String> map:stringMaps) {
 System.out.println(map.get("photo"));
}

EDIT

Imports:

import com.google.gson.Gson;
import com.google.gson.internal.StringMap;
import com.google.gson.reflect.TypeToken;
answered Aug 17, 2012 at 13:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.