JSON Return Giving Error

Asked

Viewed 109 times

0

I’m doing a service to get the return JSON in an Android application, so far so good, but I don’t know where I’m going wrong when it comes to getting the value.

The structure of JSON is this:

"entity": {
    "code": 1,
    "message": "Sucesso",
    "clienteModel": {
        "codigoCliente": 1,
        "nomeCliente": "ederson",
        "rgCliente": "448868",
        "cpfCliente": "11212444488",
        "dataNascimentoCliente": 614833200000,
        "emailCliente": "eder",
        "dataCadastroCliente": 1504666800000,
        "codigoEndereco": {
            "code": 1,
            "message": "Sucesso",
            "codigoEndereco": 1,
            "nomeEndereco": "teste",
            "complementoEndereco": "sxasdf",
            "estadoEndereco": "sdafasd",
            "cidadeEndereco": "sdfasd",
            "bairroEndereco": "sdasds",
            "numeroEndereco": 11211,
            "numeroCEPEndereco": "06851020"
        }
    }
},

This is my method where I read JSON, when I try to get the codeClient it does not find, what am I doing wrong? the code and the message I can pick up from the other fields no can pick up.

private ClienteSaidaDTO getClienteSaidaDTOs(String jsonString) {

    ClienteSaidaDTO trends = new ClienteSaidaDTO();

    try {
        JSONObject jsonObjectConvertString = new JSONObject(jsonString);
        JSONObject jsonObjectEntity = jsonObjectConvertString.getJSONObject("entity");
        //JSONObject jsonObjectClienteModel = jsonObjectConvertString.getJSONObject("clienteModel");

        ClienteSaidaDTO clienteSaidaDTO = new ClienteSaidaDTO();
        ClienteModel clienteModel = new ClienteModel();

        clienteSaidaDTO.setCode(jsonObjectEntity.getInt("code"));
        clienteSaidaDTO.setMessage(jsonObjectEntity.getString("message"));

        clienteModel.setCodigoCliente(jsonObjectEntity.getInt("codigoCliente"));

        clienteSaidaDTO.setClienteModel(clienteModel);

        trends = clienteSaidaDTO;
        //}
    } catch (JSONException e) {
        Log.e("DEVMEDIA", "Erro no parsing do JSON", e);
    }

    return trends;
}

1 answer

0

It is because the "codeClient" is inside the "clienteModel" array, so you must first take the "clienteModel" array".

String[] clienteModel = jsonObjectEntity.getStringArray("clienteModel");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.