json错误“不是JSONObject”:以字符串形式返回此格式的API。如何使用Java从中读取和创建对象?
这是请求:https://api-pub.bitfinex.com/v2/candles/trade:1h:tBTCUSD/hist?limit=2
答案是:
[[1607630400000,18399,18415,18450.367075,18399,279.63699634], [1607626800000,18290.48824022,18399,18400,18255,190.53601166]]
在另一篇帖子中,有人告诉我这是一个Json。。。但当我尝试这个:
public static void main(String[] args) throws IOException {
String url = "https://api-pub.bitfinex.com/v2/candles/trade:1h:tBTCUSD/hist?limit=2";
try {
URL urlObj = new URL(url);
HttpURLConnection conexion2 = (HttpURLConnection) urlObj.openConnection();
conexion2.setRequestProperty("Accept-Language", "UTF-8");
conexion2.setRequestMethod("GET");
conexion2.connect();
InputStreamReader in2 = new InputStreamReader(conexion2.getInputStream());
BufferedReader br2 = new BufferedReader(in2);
String output;
output = br2.readLine();
JSONArray array = new JSONArray(output);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
输出为:
org.json.JSONException: JSONArray[0] is not a JSONObject.
也许我不需要将这个字符串转换成Json?但如何将该字符串转换为数组或列表
谢谢
# 1 楼答案
根据你的回答
jsonArray中没有json对象,而是有另一个json数组。下面是一个可以工作的代码片段。必须在外观中获取JSONArray,然后在内部数组中获取元素。这是我对你们努力实现的理解