user2144555
user2144555

Reputation: 1313

Get JSON values to form an Object

Using GSON :

Gson gson = new Gson();
String json = gson.toJson(response);
System.out.println(json);

I receive the following JSON representation of a User:

"{\"userID\":\"user2\",\"firstName\":\"Maria\",\"lastName\":\"Silva\",\"birthDate\":\"Ago 1, 2012\",\"gender\":\"Female\"}"

Now, I want to get those values to construct a User object (doing User.setuserID, userObj.setFirstName, ... )

How can I get the correspond values to set the User values?

Upvotes: 0

Views: 60

Answers (1)

Rahul
Rahul

Reputation: 45070

Gson will do that for you. You need not worry about it. That's the power of Gson.

User object = gson.fromJson(jsonString, User.class); // Fully populated User object.

Upvotes: 1

Related Questions