我正在构建一个 Android 应用程序,它使用 HTTP 请求来使用 Web API。我已经很容易地发出了请求,一切正常,但我找不到一种方法将我正在获取的 JSONArray 转换为单个 Float x、Float y 值。 欢迎任何帮助。
API 的外观:
我的代码:
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject jsonObj = new JSONObject(response.body().string());
// Getting JSON Array node
JSONArray price = jsonObj.getJSONArray("price");
for(int I = 0; I < price.length(); I++) {
Float x ;
Float y ;
values.add(new Entry(x,y)); // add one entry per hour
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
最佳答案
您可以使用以下方式:
JsonArray jArr = price.getJSONArray(I);
values.add(new Entry((float)jArr.getDouble(0), (float)jArr.getDouble(1)));
此外,您需要检索的 price
应该位于 jsonObject 中,而不是另一个数组中。
关于java - 如何将 JSONArray 转换为字符串/浮点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46229039/