Home > database >  Problem extracting specific values from API using JSON
Problem extracting specific values from API using JSON

Time:11-24

I am experiencing a problem getting data from an API and was wondering if someone could help please. I basically the value 'low_14h' has a number of responses in different currencies. I would just like to be able to extract the value in 'usd' and put it into my textbox 'dailylowtext'. I can get close with fiddling around and get some of the values from 'low_24h', but am struggling to just about get the value from 'usd'. Hopefully this makes sense what it is I'm trying to achieve.

My current code is:

RequestQueue queue = Volley.newRequestQueue(this); String url = "https://api.coingecko.com/api/v3/coins/mina-protocol";

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONObject jObj=new JSONObject(response.toString());
                String usd = jObj.getJSONObject("response").getJSONObject("low_24h").getString("usd").toString();



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



            // TODO Auto-generated method stub
            dailylowtext.setText(response.toString());
            System.out.println("Response => " response.toString());


        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            // TODO Auto-generated method stub

        }
    });

    queue.add(jsObjRequest);

CodePudding user response:

I think you forgot the "market_data". In the JSON's file, market_data is the parent of low_24.

enter image description here

You don't have a "response" node in your JSON's data, try with market_data instead of response.

String usd = jObj.getJSONObject("market_data").getJSONObject("low_24h").getString("usd").toString();

You can also use library such as gson or moshi that will help you to work with JSON data.

  • Related