Return reading in JSON

8

I can read json's return in the format

[{"celular":"123456","_id":"1"}]

The code that works with json above is this:

 public static void MakeJsonArrayReq() {

    JsonArrayRequest jreq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject jo = response.getJSONObject(i);

                            int _id = jo.getInt("_id");
                            String celular = jo.getString("celular");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                 }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });

    MyApplication.getInstance().addToReqQueue(jreq, "jreq");
}

I can not read json in the format:

{"carro":[{"celular":"123456","_id":"1"}]}

How can I adapt the MakeJsonArrayReq () method to read the json return above?

    
asked by anonymous 11.10.2015 / 16:46

2 answers

1

Dude, use Jackson to parse this JSON, you create an object that represents JSON, it will make it much easier. In case it will be an X object with a list of Car objects, and inside the Car object you have Cellular and Id.

An interesting link with some information from Jackson: link

Here is an example of using Jackson: (Do not forget to add Jackson's dependency on pom.xml)

public class App {
    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = "{" + "\"nomeDono\":\"stackoverflow\"," + "\"carros\":[" + "{\"id\":1,\"modelo\":\"ford\"}," + "{\"id\":2,\"modelo\":\"bmw\"}" + "]" + "}";
        JSON jsonObject = objectMapper.readValue(json, JSON.class);
        printJson(jsonObject);
    }

    private static void printJson(JSON jsonObject) {
        System.out.println("Nome do dono é " + jsonObject.getNomeDono());
        for (Carro carro : jsonObject.getCarros()) {
            System.out.println("Carro " + carro.getId() + " e modelo " + carro.getModelo());
        }
    }
}

public class JSON {

    private String nomeDono;
    private List<Carro> carros;

    @JsonCreator
    public JSON(@JsonProperty("nomeDono") String nomeDono, @JsonProperty("carros") List<Carro> carros) {
        this.nomeDono = nomeDono;
        this.carros = carros;
    }

    public String getNomeDono() {
        return nomeDono;
    }
    public void setNomeDono(String nomeDono) {
        this.nomeDono = nomeDono;
    }
    public List<Carro> getCarros() {
        return carros;
    }
    public void setCarros(List<Carro> carros) {
        this.carros = carros;
    }
}

public class Carro {

    private Long id;
    private String modelo;

    @JsonCreator
    public Carro(@JsonProperty("id") Long id, @JsonProperty("modelo") String modelo) {
        this.id = id;
        this.modelo = modelo;
    }

    public Long getId() {
        return id;
    }
    public String getModelo() {
        return modelo;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public void setModelo(String modelo) {
        this.modelo = modelo;
    }
}
    
28.10.2015 / 09:51
1

Rule :

{ } = JSONObject

[ ] = JSONArray

So :

String json = "{\"carro\":[{\"celular\":\"123456\",\"_id\":\"1\"}]}";

try {

    JSONObject jo = new JSONObject(json);
    String carro = jo.getString("carro");

    JSONArray ja = new JSONArray(carro);
    JSONObject var = ja.getJSONObject(0);

    int _id = var.getInt("_id");
    String celular = var.getString("celular");

} catch (JSONException e) {
    e.printStackTrace();
}
    
24.12.2018 / 11:12