getting null JSONObject values

2019-09-20 01:43发布

I'm trying to create a JSONObject as code below. But Android Studio is saying that it's null. Where is my mistake?

I tried two different ways to create it.

1st

String JSONString = "{" +
            "  \"retorno\": {" +
            "    \"empresas\": [" +
            "      {" +
            "        \"cnpj\": \"05.743.645/0001-38\"," +
            "        \"razao_social\": \"GISELA TRANSPORTES E DISTRIBUIDORA DE FLORES LTDA - ME\"," +
            "        \"endereco\": \"EST RSC-453 (ROTA DO SOL) KM 93,8\"," +
            "        \"bairro\": \"BAIRRO ALFANDEGA\"," +
            "        \"numero\": 26," +
            "        \"complemento\": \"\"," +
            "        \"telefone\": \"3462 2749\"," +
            "        \"celular\": \"\"," +
            "        \"email\": \"giselaflores@giselaflores.com.br\"" +
            "      }" +
            "    ]" +
            "  }" +
            "}";
try {
        JSONObject jsonEmpresa = new JSONObject(JSONString);
        String email = jsonEmpresa.getString("email");
} catch (JSONException e) {
        e.printStackTrace();
    }

2nd

try {
        JSONObject jsonEmpresa = new JSONObject();
        jsonEmpresa.put("cnpj", "05.743.645/0001-38");
        jsonEmpresa.put("razao_social", "GISELA TRANSPORTES E DISTRIBUIDORA DE FLORES LTDA - ME");
        jsonEmpresa.put("endereco", "EST RSC-453 (ROTA DO SOL) KM 93,8");
        jsonEmpresa.put("bairro", "BAIRRO ALFANDEGA");
        jsonEmpresa.put("numero", 26);
        jsonEmpresa.put("complemento", "");
        jsonEmpresa.put("telefone", "3462 2749");
        jsonEmpresa.put("celular", "");
        jsonEmpresa.put("email", "giselaflores@giselaflores.com.br");

        String email = jsonEmpresa.getString("email");
} catch (JSONException e) {
        e.printStackTrace();
    }

String email's value is null, it should be giselaflores@giselaflores.com.br.

When I tried to debug, I had the message jsonEmpresa: "null".

1条回答
Deceive 欺骗
2楼-- · 2019-09-20 02:34

To get email value for given example, you should to do like

 String JSONString = "{" +
                "  \"retorno\": {" +
                "    \"empresas\": [" +
                "      {" +
                "        \"cnpj\": \"05.743.645/0001-38\"," +
                "        \"razao_social\": \"GISELA TRANSPORTES E DISTRIBUIDORA DE FLORES LTDA - ME\"," +
                "        \"endereco\": \"EST RSC-453 (ROTA DO SOL) KM 93,8\"," +
                "        \"bairro\": \"BAIRRO ALFANDEGA\"," +
                "        \"numero\": 26," +
                "        \"complemento\": \"\"," +
                "        \"telefone\": \"3462 2749\"," +
                "        \"celular\": \"\"," +
                "        \"email\": \"giselaflores@giselaflores.com.br\"" +
                "      }" +
                "    ]" +
                "  }" +
                "}";
        try {
            JSONObject jsonEmpresa = new JSONObject(JSONString);
            JSONObject retorno = jsonEmpresa.getJSONObject("retorno");
            JSONArray empresas = retorno.getJSONArray("empresas");
            JSONObject empresa =  empresas.getJSONObject(0);

            String email =empresa.getString("email");

        } catch (JSONException e) {
            e.printStackTrace();
        }
查看更多
登录 后发表回答