chirag90
chirag90

Reputation: 2240

How to parse nested JSON using GSON

I am currently making a get request using volley and in onResponse i am trying to parse the jsonObject using gson to my model.

JSON Returned after the request is made:

{
   "success": 1,
   "message": "Done",
   "data": {
             "company": "companyname",
             "email": "email",
             "color": "#ffffff",
             "text_color": "#000000",
             "background_image": "BITMAP ENCODED",
             "logo": "BITMAP ENCODED",
             "enable_health_and_safety": 0,
             "health_and_safety": "",
             "sign_in_button_text": "SIGN IN",
             "sign_out_button_text": "SIGN OUT",
             "enable_picture": 0,
             "package_type": "TRIAL",
             "created_at": "2017-03-06 12:21:23",
             "updated_at": "2017-03-06 12:21:23",
             "telephone": "",
             "auto_sign_out": 0,
             "sign_out_time": "",
             "enable_printing": 0
          }
}

My Model

public class ClientResponseModel implements Serializable {
private String success;
private String message;
private ClientData clientData;

public String getSuccess() {
    return success;
}

public void setSuccess(String success) {
    this.success = success;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public ClientData getClientData() {
    return clientData;
}

public void setClientData(ClientData clientData) {
    this.clientData = clientData;
}

public class ClientData implements Serializable {
    private String company;
    private String email;
    private String client_color;
    private String text_color;
    private String background_image;
    private String logo;
    private int enable_health_and_safety;
    private String health_and_safety;
    private String sign_in_button_text;
    private String sign_out_button_text;
    private int enable_picture;
    private String package_type;
    private String created_at;
    private String updated_at;
    private String telephone;
    private int auto_sign_out;
    private String sign_out_time;
    private int enable_printing;

    //all the getters and setters for above 
}

My onResponse function

final JsonObjectRequest getClientData = new JsonObjectRequest(Request.Method.GET, clientUrl, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.i("getUserDataOnResponse", response.toString());
            Gson gson = new Gson();
            ClientResponseModel clientResponseModel = gson.fromJson(response.toString(), ClientResponseModel.class);


            Log.i("sucess",clientResponseModel.getSuccess());
            Log.i("message",clientResponseModel.getMessage());
            Log.i("company",clientResponseModel.getClientData().getEmail());

        }

I can get the success and message however when trying to get the data from clientData, Forexample getting the email. The app crashes and i get a following error message.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ClientResponseModel$ClientData.getEmail()' on a null object reference

I am not sure what i could be doing wrong, i have had looked at few links for example How to use gson and volley parse nested json

Thanks for all the help in advance.

Upvotes: 0

Views: 3227

Answers (3)

user8636357
user8636357

Reputation:

As Mentioned by user1912026, change as below.

private ClientData data;

And change the setter and getter as below

public ClientData getClientData() {
    return data;
}

public void setClientData(ClientData clientData) {
    this.data = clientData;
}

Upvotes: 1

user1912026
user1912026

Reputation: 106

There is a little mistake in your code.So you should change ClientData clientData to ClientData data. Then you can get the data.Hope this will help you in parsing.

Upvotes: 1

Hamid Goodarzi
Hamid Goodarzi

Reputation: 1394

i have a really easy approach for you:

first of all create the main model: and the other one for "data"

public class FeedBackModel {
    int success;
    String message;
    DataModel data;

}

public class DataModel {

    String company;
    String email;
    //etc ...
}

// create getter& setter for variables

then when you got json, parse it like this:

            Gson gson = new Gson();
            Type CollectionType = new TypeToken<FeedBackModel>() {
            }.getType();
            FeedBackModel FeedBack = gson.fromJson(json, CollectionType);

Upvotes: 1

Related Questions