How to parse nested JSON using GSON

痞子三分冷 提交于 2019-12-31 05:13:04

问题


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.


回答1:


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.




回答2:


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);



回答3:


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;
}


来源:https://stackoverflow.com/questions/42652448/how-to-parse-nested-json-using-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!