获取空的JSONObject值 [英] getting null JSONObject values

查看:155
本文介绍了获取空的JSONObject值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个JSONObject,如下所示.但是Android Studio表示它为空.我的错误在哪里?

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.

第一

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

第二

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

字符串电子邮件的值为 null ,应为 giselaflores@giselaflores.com.br .

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

当我尝试调试时,收到消息 jsonEmpresa:空" .

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

推荐答案

要获取给定示例的电子邮件值,您应该这样做

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

这篇关于获取空的JSONObject值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆