0

Below are the two urls which returns json response

1) https://jsonplaceholder.typicode.com/users

2) https://jsonplaceholder.typicode.com/posts

When user hit the first url ,I want to see all users information with their related posts. Note:id of users is present in posts response.

I need java code to parse this response.

1 Answers1

1

I suggest using Google's gson for serialization from/to json. You can find the latest version of gson here

First add this dependency to your pom.xml

Then, create in your implementation create a new Gson object:

Gson gson = new Gson();

Lets assume that you created the User model and a Users class which encapsulates User list, what you will do is:

List<User> users = gson.fromJson(responseString, Users.class);

After this point you can convert your Users object to any data structure you desire using Java streams.

You can do the vice versa by using toJson method of Gson.

ahmetcetin
  • 1,824
  • 18
  • 30