0

I am looking for approach.

I am parsing in JSON which is returning data in form

UserA  PhotoURL1  
UserA  PhotoURL2
UserB  PhotoURL3
UserA  PhotoURL4
UserB  PhotoURL5
UserA  PhotoURL6

I need to store it in My Custom Array List

String name,
List<Photo> photos

So result should be

UserA   Photo1 
        Photo2
        Photo4
        Photo6

UserB   Photo3
        Photo5

What approach should I take? Please help

4
  • What does your JSON look like exactly? What JSON library do you use, if any? Commented Dec 22, 2012 at 12:50
  • 1
    What code have you already written? Commented Dec 22, 2012 at 12:50
  • 2
    apart from the two comments above, you show us that you are not expecting an ArrayList, but a Map.. Commented Dec 22, 2012 at 12:52
  • Hi , Parsing of JSON is not an issue for me. To store data in correct data structure is the issue. Kent , Do you think i need a map to store and dispaly data in this form Commented Dec 22, 2012 at 13:10

1 Answer 1

2

Populating a map (as @Kent suggests) from the given data is usually done using something like this:

Map<String, List<Photo>> result = new HashMap<String, List<Photo>>();

while(data.hasMoreData()) {

  String name = data.getName();
  String photo = data.getPhoto();

  List<Photo> photos = result.get(name);
  if (photos == null) {
    photos = new ArrayList<Photo>();
    result.put(name, photos);
  }
  photos.add(photo);

}

System.out.println(result);

In any event we would need to understand how the original data is json formatted to do this.

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

2 Comments

Thanks , I am trying your answer.
You saved my day. You are genious. Hats off to you.

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.