I am trying to convert the http response body which is in JSON format into a Java object using the GSON library, but the object has all attributes equal to null after trying to do so.
My Object class:
public class User {
private String username;
private int shipCount;
private int structureCount;
private String joinedAt;
private int credits;
public User(String username, int shipCount, int structureCount, String joinedAt, int credits) {
this.username = username;
this.shipCount = shipCount;
this.structureCount = structureCount;
this.joinedAt = joinedAt;
this.credits = credits;
}
Plus getters and setters
My attempt at using GSON:
Gson gso = new Gson();
User userInf = gso.fromJson(response.body(), User.class);
System.out.println(userInf);
The response body is this:
{"user":{"username":":chrter","shipCount":0,"structureCount":0,"joinedAt":"2022-04-09T16:52:14.365Z","credits":0}}
Any help is greatly appreciated
CodePudding user response:
Try something like this:
public static Map<String, Object> Converter(String str){
Map<String, Object> map = new Gson().fromJson(str, new TypeToken<HashMap<String, Object>>() {}.getType());
return map;
}
Map<String, Object> apiResponse = Converter(response.body().toString());
Map<String, Object> username = Converter(apiResponse.get("user").toString());
System.out.println(username);
Tweak it a bit to suit your need
CodePudding user response:
The root object of your HTTP response is a JSON object that has only one field - "user"
. When GSON deserializes the response, it goes through all the fields of the root object and sets them to the corresponding fields of the class you have provided. So it will look for the field user
in the class User
and set it to the JSON's data.
Since class User
has no field user
, GSON doesn't fill that field. In fact it doesn't fill out any other fields, because there are no other fields in the root object.
To get around this you need to deserialize not the whole response, but only the user
field of the root object. There are two ways of doing that.
Deserialize the root object, and then deserialize the field
user
of that object into theUser
class.You can find an example of how to do it in @Saheed's answer. However you should note that it translates JSON string into java objects and then translates java objects back. It might cost you additional time if you're doing it in performance-sensitive area of your program.
Create another class which you will deserialize into, which would have the field user. It looks like this:
class Response { public User user; }; class User { // ... };
And then you deserialize like this:
Gson gso = new Gson(); // CHANGE: Deserialize the response and get the user field Response response = gso.fromJson(response.body(),Response.class); User userInf = response.user; System.out.println(userInf);
CodePudding user response:
Try this.
Map<?, ?> map = gson.fromJson(response.body(), Map.class);
for (Map.Entry<?, ?> entry : map.entrySet()) {
System.out.println(entry.getKey() "=" entry.getValue());
}