如何使用GSON解析嵌套的JSON

时间:2017-03-07 15:42:56

标签: android json gson android-volley

我目前正在使用volley进行get请求,而在onResponse中我正在尝试使用gson将jsonObject解析为我的模型。

JSON在发出请求后返回:

{
   "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
          }
}

我的模特

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 
}

我的onResponse功能

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

        }

我可以获得成功和消息但是当尝试从clientData获取数据时,例如获取电子邮件。该应用程序崩溃,我收到以下错误消息。

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

我不确定我的错误是什么,我已经查看了一些链接,例如How to use gson and volley parse nested json

提前感谢所有帮助。

3 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误。因此,您应该将ClientData clientData更改为ClientData数据。然后你就可以获得数据。希望这可以帮助你解析。

答案 1 :(得分:0)

我有一个非常简单的方法:

首先创建主模型: 另一个用于&#34;数据&#34;

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

}

public class DataModel {

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

//创建getter&amp;变量的setter

然后当你得到json时,解析它就像这样:

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

答案 2 :(得分:0)

如user1912026所述,更改如下。

private ClientData data;

并按以下方式更改setter和getter

public ClientData getClientData() {
    return data;
}

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