Return reading in JSON

Asked

Viewed 927 times

7

I can read the return of json in the format

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

The code that works with the 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 cannot read json in the format:

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

How can I adapt the Makejsonarrayreq() method to read the json feedback above ?

  • In the first JSON you have a JSONArray, in the second is a JSONObject.

  • And complementing what @Renan said, you need to use a JsonObjectRequest instead of JsonArrayRequest.

  • I tried to implement the code with Jsonobjectrequest and it didn’t work. How could I leave the code in use with Jsonobjectrequest?

2 answers

1

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

An interesting link with some information from Jackson: http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial

Follow an example of Jackson’s use: (Don’t forget to add the Jackson 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;
    }
}
  • 1

    Ola Lucas. Instead of putting the link to the tutorial, an example of how to use the Jackson in his reply. Otherwise his reply would be more useful if it were converted into a commentary.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.