JSON parsing with android volley

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 do not 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);

That way it does not return the value, what should I do to fix it?

    
asked by anonymous 02.01.2017 / 02:48

1 answer

1
So, if there is a "[]" before and after the return, it means that JSON is an Array, so what you need to do (and most likely solve your problem) is to switch from JSONObject to array , and then get the 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);
    
02.01.2017 / 11:52