Parsing JSON with Volley android

Asked

Viewed 309 times

1

I have this JSON on the server side:

[{"idreg":"271896",
"code":"USD",
"codein":"BRL",
"name":"D\u00f3lar Comercial",
"high":"3.2652",
"pctChange":"-0.939",
"open":"0",
"bid":"3.2492",
"ask":"3.2497"}]

I want to use the value of "Ask" but I don’t know exactly how to interpret it

Here is my code:

JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, url, new 

Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject obj = response.getJSONObject("ask");


            }
           JSONcatch (JSONException e) {

                e.printStackTrace();
            }
        }
    },null);

This way it does not return me the value, what I must do to correct?

1 answer

1


So, if you notice there is a "[ ]" before and after the return, it means that JSON is an Array, so what you need to do (and it will probably solve your problem), is to switch from Jsonobject to array, and then take Jsonobject

JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, url, new 

Response.Listener<JSONArray>() {

    @Override
    public void onResponse(JSONArray response) {
        try {
            JSONObject obj = response.getJSONObject(0);
            Double ask = obj.getDouble("ask");
        }
       JSONcatch (JSONException e) {

            e.printStackTrace();
        }
    }
},null);

Browser other questions tagged

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