亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取空的JSONObject值

獲取空的JSONObject值

開心每一天1111 2021-04-01 15:14:26
我正在嘗試創建一個JSONObject作為下面的代碼。但是Android Studio表示它為空。我的錯誤在哪里?我嘗試了兩種不同的方式來創建它。1號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\": \"[email protected]\"" +            "      }" +            "    ]" +            "  }" +            "}";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", "[email protected]");        String email = jsonEmpresa.getString("email");} catch (JSONException e) {        e.printStackTrace();    }字符串電子郵件的值為null,應為[email protected]。當我嘗試調試時,我收到消息jsonEmpresa:“ null”。
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

要獲得給定示例的電子郵件價值,您應該喜歡


 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\": \"[email protected]\"" +

                "      }" +

                "    ]" +

                "  }" +

                "}";

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

        }


查看完整回答
反對 回復 2021-04-18
  • 1 回答
  • 0 關注
  • 283 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號