getting null JSONObject values

半腔热情 提交于 2019-12-13 08:18:26

问题


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:


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


来源:https://stackoverflow.com/questions/50948797/getting-null-jsonobject-values

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