Prateekro Prateekro - 3 months ago 12
Java Question

Correct GSON call to convert from JSON objects to Java objects

Please suggest the correct method to call GSON here to convert to the java object.

final JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("user",user);
jsonBody.put("password",pass);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, "CheckForResponse: "+jsonBody.toString());
JsonObjectRequest JSONObjectRequest = new JsonObjectRequest(AppConfig.URL_LOGIN, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

Log.d("Response",response.toString());

//How to call GSON here?

Token token = new Gson().fromJson(String.valueOf(response), Token.class);

Token token1 = new Token();
User user1 = new User();
Log.d(TAG, "Triplet: onCreate: "+token1.getToken());
Log.d(TAG, "Triplet: onCreate: "+user1.getUsername());

Toast.makeText(TLogin.this, "InsideResponse : Welcome", Toast.LENGTH_SHORT).show();
Intent goToMainScreen = new Intent(TLogin.this,GameMenu.class);
startActivity(goToMainScreen);
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error != null && error.getMessage() != null)
{
Log.e("Response error", error.getMessage());
}
}
}
);
AppController.getInstance().addToRequestQueue(JSONObjectRequest);


post script: I have created POJO classes for the getter and setter for java object from the JSON.



JSON data

{
"token":"ezI1NiJ9.eyJ1c2Vn0.028Kmtp",

"user":
{
"id":"57a5ff5",
"avatar":10,
"username":"abc",
"rating":12,
"facebookPicture":"https:\/\/graph.facebook.com\/v2.5\/11642925\/picture?type=square&width=200&height=200"
}
}


POJO Class
Token.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Token {

@SerializedName("token")
@Expose
private String token;
@SerializedName("user")
@Expose
private User user;

/**
*
* @return
* The token
*/
public String getToken() {
return token;
}

/**
*
* @param token
* The token
*/
public void setToken(String token) {
this.token = token;
}

/**
*
* @return
* The user
*/
public User getUser() {
return user;
}

/**
*
* @param user
* The user
*/
public void setUser(User user) {
this.user = user;
}

}


User.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class User {

@SerializedName("id")
@Expose
private String id;
@SerializedName("avatar")
@Expose
private Integer avatar;
@SerializedName("username")
@Expose
private String username;
@SerializedName("rating")
@Expose
private Integer rating;
@SerializedName("facebookPicture")
@Expose
private String facebookPicture;

/**
*
* @return
* The id
*/
public String getId() {
return id;
}

/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}

/**
*
* @return
* The avatar
*/
public Integer getAvatar() {
return avatar;
}

/**
*
* @param avatar
* The avatar
*/
public void setAvatar(Integer avatar) {
this.avatar = avatar;
}

/**
*
* @return
* The username
*/
public String getUsername() {
return username;
}

/**
*
* @param username
* The username
*/
public void setUsername(String username) {
this.username = username;
}

/**
*
* @return
* The rating
*/
public Integer getRating() {
return rating;
}

/**
*
* @param rating
* The rating
*/
public void setRating(Integer rating) {
this.rating = rating;
}

/**
*
* @return
* The facebookPicture
*/
public String getFacebookPicture() {
return facebookPicture;
}

/**
*
* @param facebookPicture
* The facebookPicture
*/
public void setFacebookPicture(String facebookPicture) {
this.facebookPicture = facebookPicture;
}

}

Answer

Insted of

Log.d(TAG, "Triplet: onCreate: "+token1.getToken());
Log.d(TAG, "Triplet: onCreate: "+user1.getUsername());

use

Log.d(TAG, "Triplet: onCreate: "+token.getToken());
Log.d(TAG, "Triplet: onCreate: "+token.getUser().getUsername());
Comments