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".