我在從 API 獲取資料時遇到問題,想知道是否有人可以提供幫助。我基本上'low_14h'的值有許多不同貨幣的回應。我只想能夠提取“usd”中的值并將其放入我的文本框“dailylowtext”中。我可以接近擺弄并從“low_24h”中獲取一些值,但我正在努力從“usd”中獲取值。希望這對我正在努力實作的目標有意義。
我目前的代碼是:
RequestQueue 佇列 = 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);
uj5u.com熱心網友回復:
我想你忘記了“market_data”。在 JSON 檔案中,market_data 是 low_24 的父級。

您的 JSON 資料中沒有“回應”節點,請嘗試使用 market_data 而不是回應。
String usd = jObj.getJSONObject("market_data").getJSONObject("low_24h").getString("usd").toString();
您還可以使用gson或moshi等庫來幫助您處理 JSON 資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364483.html
