java - tutorial - xml string to json using gson




How to convert Json to Java object using Gson (3)

This question already has an answer here:

Suppose I have json string

{"userId":"1","userName":"Yasir"}

now I have a class User

class User{
int userId;
String userName;
//setters and getters
}

Now How can I convert above json string to user class object


Try this:

Gson gson = new Gson();
String jsonInString = "{\"userId\":\"1\",\"userName\":\"Yasir\"}";
User user= gson.fromJson(jsonInString, User.class);

Gson gson = new Gson();

User u=gson.fromJson(jsonstring, User.class);
System.out.println("userName: "+u.getusername);  

User user= gson.fromJson(jsonInString, User.class);

// where jsonInString is your json {"userId":"1","userName":"Yasir"}






gson