-3

I have an arrayList after parsing JSON:

movieList = new ArrayList();

                    HashMap<String, String> one = new HashMap<>();

                    one.put("title", title);
                    one.put("popularity", String.valueOf(popularity));
                    one.put("poster", poster);

                    movieList.add(one);

movieList looks like this in the log:  [{popularity=224, poster=/dM2w364MScsjFf8pfMbaWUcWrR.jpg, title=Pulp Fiction}]

I need to put those values in a custom Movie object that has popularity, poster and title. Then I need to create an array of those objects.

I dont know how to do that. The above code is in onPostExecute. How to set those 3 values from log as values for my Movie objects ?

Hele Nova
  • 7
  • 2
  • You can use a library like [gson](https://github.com/google/gson) to do this. – D.B. Nov 08 '17 at 02:19
  • `[{popularity=224, poster=/dM2w364MScsjFf8pfMbaWUcWrR.jpg, title=Pulp Fiction}]` this is an array that containing objects. Why you need another custom object? still not clear what the last output that you want. – Sh4m Nov 08 '17 at 02:27
  • I got this list to do: 1.Extract the JSON object data (title, popularity etc.) - done 2.Create a new Movie object - done in a class Movie.java where I have the constructor too 3.Set the data (extracted from JSON object) in new movie object - here is my problem. I dont know what to do. 4.add the movie object to the movies ArrayList – Hele Nova Nov 08 '17 at 02:33

1 Answers1

0

one is a HashMap instance, and then you're adding one to your ArrayList, which is probably not what you want.

Going off of your comment:

1.Extract the JSON object data (title, popularity etc.) - done 2.Create a new Movie object - done in a class Movie.java where I have the constructor too 3.Set the data (extracted from JSON object) in new movie object - here is my problem. I dont know what to do. 4.add the movie object to the movies

The Movie.java file is the movie object, so I think in step 3) it sounds like you need to use the constructor that you made in step 2), That will create movie objects, and you should probably do that for every movie that you have JSON data for, and as you create the objects you should add them to your List<Movie> movieList.

Blake
  • 986
  • 9
  • 24
  • Blake, thank you for your answer. How to access values from JSON. I pared it in onPostExecute , does it mean that I also need to create objects within onPostExecute? Values : title, poster, popularity from JSON are not accessible in onCreate. – Hele Nova Nov 08 '17 at 03:51
  • I figured it out. The solution was to create array list of objects within the class getStringsFromJson() and change the Async Task signature to ArrayList to get the array list as a result in onPostExecute. – Hele Nova Nov 09 '17 at 01:49