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 aJSONObject
.– Renan Gomes
And complementing what @Renan said, you need to use a
JsonObjectRequest
instead ofJsonArrayRequest
.– Wakim
I tried to implement the code with Jsonobjectrequest and it didn’t work. How could I leave the code in use with Jsonobjectrequest?
– robsonp