Suppose I have a json string
{"userId":"1","userName":"Yasir"}
I now have a User class
class User{ int userId; String userName; //setters and getters }
Now How to convert above json string to user class object
Try the following:
Gson gson = new Gson(); String jsonInString = "{\"userId\":\"1\",\"userName\":\"Yasir\"}"; User user= gson.fromJson(jsonInString, User.class);
User user= gson.fromJson(jsonInString, User.class); // where jsonInString is your json {"userId":"1","userName":"Yasir"}
Gson gson = new Gson(); User u=gson.fromJson(jsonstring, User.class); System.out.println("userName: "+u.getusername);
Gson gson = new Gson(); User user = gson.fromJson("{\"userId\":\"1\",\"userName\":\"Yasir\"}", User.class));