You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用CoinDesk API遇JsonObject {"time"}未找到问题咨询

解决CoinDesk API JSON解析报错:JsonObject {"time"} not found

我来帮你一步步排查这个问题!这种报错通常要么是JSON结构理解错了,要么是响应没被正确解析成JSON对象,咱们一个个来梳理:

1. 先明确CoinDesk API的典型响应结构

以常用的比特币当前价格API(https://api.coindesk.com/v1/bpi/currentprice.json)为例,它返回的JSON是嵌套结构,time本身是一个子对象,而不是直接的字符串值:

{
  "time": {
    "updated": "Sep 18, 2024 14:20:00 UTC",
    "updatedISO": "2024-09-18T14:20:00+00:00",
    "updateduk": "Sep 18, 2024 15:20:00 BST"
  },
  "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
  "chartName": "Bitcoin",
  "bpi": {
    "USD": {
      "code": "USD",
      "symbol": "$",
      "rate": "52,145.9125",
      "description": "United States Dollar",
      "rate_float": 52145.9125
    }
  }
}

如果你试图直接把time当成一个普通值来获取(比如想直接取它的字符串),或者代码里访问路径写错了,就会触发找不到key的错误。

2. 检查响应是否被正确解析为JSON对象

这是最常见的问题!很多时候开发者拿到的是原始的响应字符串,却直接当成JSON对象去操作,自然找不到任何key。举几个常见语言的正确解析方式:

Python示例

import requests
import json

# 发送请求
response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")

# 先确认请求成功(状态码200)
if response.status_code == 200:
    # 把响应文本转换成JSON字典
    data = json.loads(response.text)
    # 正确获取time下的子字段
    updated_time = data["time"]["updated"]
    print(f"最近更新时间:{updated_time}")
else:
    print(f"请求失败,状态码:{response.status_code}")
    # 打印原始响应看看是不是错误页面(比如404的HTML)
    print(response.text)

Java示例(使用org.json库)

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.json.JSONObject;

public class CoinDeskAPI {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://api.coindesk.com/v1/bpi/currentprice.json");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            
            if (conn.getResponseCode() == 200) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                StringBuilder content = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                in.close();
                conn.disconnect();
                
                // 解析为JSON对象
                JSONObject data = new JSONObject(content.toString());
                // 获取time子对象,再取updated字段
                String updatedTime = data.getJSONObject("time").getString("updated");
                System.out.println("最近更新时间:" + updatedTime);
            } else {
                System.out.println("请求失败,状态码:" + conn.getResponseCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3. 排查其他可能的问题

  • 确认API端点是否正确:如果你调用的是其他CoinDesk API,返回的结构可能没有time字段,先手动访问API地址看看返回的JSON内容。
  • 检查请求头:确保请求时设置了Accept: application/json,有些API在没有这个头的情况下可能返回HTML而不是JSON。
  • 打印原始响应:如果还是报错,先把API返回的原始字符串打印出来,确认它是合法的JSON,而不是错误信息(比如404页面、限流提示)。

总结

报错JsonObject {"time"} not found的核心原因:
要么是你拿到的响应没被转换成JSON对象(直接操作字符串肯定找不到key),要么是你对JSON结构的理解有误(比如time是嵌套对象,或者API返回的结构里根本没有这个key)。按照上面的步骤排查,应该能很快解决问题!

内容的提问来源于stack exchange,提问作者Sahil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:12:52