简体   繁体   中英

Problem extracting specific values from API using JSON

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

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

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM