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
– Jorge Diego
Why didn’t it work? What was the mistake?
– nullptr
@nullptr the error is that the fields were all null.
– Luis Dorea
@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.
– Luis Dorea