How to convert Jsonobject to a java class

Asked

Viewed 324 times

0

Greetings to all,

I’m developing an app that takes the return of a webservice to be used in this application as login data among others. I would like to ask for your guidance on how best to convert the data that is in a Jsonobject object to a class that stores all this API return information.

Json has the structure below:

{
"token": "N02156564365034657289&(*&*&$#",
"success": true,
"timezone": "America/Bahia",
"user": {
    "name": "Fulano de roça",
    "email": "[email protected]",
    "photo": ""
},
"msg": "Autenticado com sucesso"

}

I created a class to receive the data based on this json above:

public class RetornoServer {

private String mToken;
private boolean mSuccess;
private Usuario mUser;
private String mMsg;

public RetornoServer() {
}

public RetornoServer(String Token, boolean Success, Usuario User, String Msg) {
    this.mToken = Token;
    this.mSuccess = Success;
    this.mUser = User;
    this.mMsg = Msg;
}

public String getmToken() {
    return mToken;
}

public void setmToken(String mToken) {
    this.mToken = mToken;
}

public boolean ismSuccess() {
    return mSuccess;
}

public void setmSuccess(boolean mSuccess) {
    this.mSuccess = mSuccess;
}

public Usuario getmUser() {
    return mUser;
}

public void setmUser(Usuario mUser) {
    this.mUser = mUser;
}

public String getmMsg() {
    return mMsg;
}

public void setmMsg(String mMsg) {
    this.mMsg = mMsg;
}

@Override
public String toString() {
    return "RetornoServer{" +
            "mToken='" + mToken + '\'' +
            ", mSuccess=" + mSuccess +
            ", mUser=" + mUser +
            ", mMsg='" + mMsg + '\'' +
            '}';
}

}

I am using Retrofit 2 in connection with the webservice and the return is coming all right, this already feeding the Jsonobject object as shown below:

JsonObject responseJson = response.body();

I tried to convert using Gson, but it didn’t work

Gson gson = new Gson();
RetornoServer retornoServer = gson.fromJson(responseJson, RetornoServer.class);

Finally, if possible I would like your guidance on some material that I should read or some suggestion. I thank you in advance!

  • I will give you the link of a site that I always use when I want to do this, it’s very simple, just paste the Json and it returns you the Java class, however it uses the annotations of the Jacksonannotations library. Follow the link: Json Schema to Pojo

  • Why didn’t it work? What was the mistake?

  • @nullptr the error is that the fields were all null.

  • @Jorgediego thank you very much, I will look at this site to see if I can solve. If I can’t, I’ll have to feed or assign the values of the objects in my hand.

1 answer

0


Gson is one of many libraries that convert JSON to objects. In your case, your attributes are all null because you could not find the fields of your object with the names of the JSON fields. For example:

public class RetornoServer {

private String token;
private boolean success;
private Usuario user;
private String msg;
(...)

More information here (taken from the Gson repository itself). You can notice that when the JSON attributes have underline (_), attributes of the Java Object also have.

  • thank you so much for the reply! Really you identified where I missed and the site you sent the link took other questions that had.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.