在改进2.0中嵌套的Json解析

时间:2016-12-13 15:18:09

标签: android json retrofit2

我在解析嵌套的json时遇到问题。我正在使用改造2.0。这是我要解析的json格式:

{
  "type": "All DONE",
  "status": "SUCCESS",
  "statusCode": "200",
  "statusMessage": "Contacts synced successfully",
  "data": {
    "__v": 0,
    "uuid": "584d632d87f0c01c44262da4",
    "mobile": "882471",
    "name": "Manit",
    "globalChecksum": "snfvsjnvrjnvlwkrnsfwlkrnfwk",
    "_id": "584d632d87f0c01c44262da8",
    "contacts": [
      {
        "name": "CC",
        "mobile": "9876281",
        "checksum": "jkndvsjlsnd",
        "_id": "584d632d87f0c01c44262da5"
      },
      {
        "name": "CC",
        "mobile": "123456789",
        "checksum": "jkndvsjlsnd",
        "_id": "584d632d87f0c01c44262da6"
      }
    ]
  }
}

这是我用来解析这个json的代码:

UsersInterface usersInterface = ApiClient.getClient().create(UsersInterface.class);
    Call<SyncTableResponse> call = usersInterface.syncContacts(json);
    call.enqueue(new Callback<SyncTableResponse>() {
        @Override
        public void onResponse(Call<SyncTableResponse> call, retrofit2.Response<SyncTableResponse> response) {
            if(response.isSuccessful()){
                Log.d("RESPONSE", String.valueOf(response.body()));

                String type = response.body().getType();
                String status = response.body().getStatus();
                String status_code = response.body().getStatus_code();
                String status_message = response.body().getStatus_message();

                Log.d("RESPONSE", type);
                Log.d("RESPONSE", status);
                Log.d("RESPONSE", status_code);
                Log.d("RESPONSE", status_message);

                JSONObject data = response.body().getData();

                Log.d("DATA", response.body().toString());
                if(status_code.equals(Constants.SUCCESS)){
                    _data = data;
                    String mobile = null;
                    try {
                        mobile = data.getString("mobile");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.d("DATA!", "Mobile: "+mobile);
                    //Toast.makeText(context, "Success!", Toast.LENGTH_SHORT).show();
                }else{
                    Log.d("DATA!", "ERROR: " + status_code);
                    //Toast.makeText(context, "ERROR: " + status_code, Toast.LENGTH_SHORT).show();
                }
            }else{
                //Toast.makeText(context, "Response Unsuccessful!", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<SyncTableResponse> call, Throwable t) {
            Log.d("RESPONSE", "FALIURE CONNECTING addUser: " + t.getMessage());
            //Toast.makeText(context, "Response Faliure!", Toast.LENGTH_LONG).show();
        }
    });

问题是,每当我使用此代码时,我都会typestatusstatusCodestatusMessage,但data始终为空。

0 个答案:

没有答案