【问题标题】:Get JSON in web api and get the specific data from JSON在 web api 中获取 JSON 并从 JSON 中获取特定数据
【发布时间】:2018-12-21 03:01:39
【问题描述】:

如何将 json 提取到 web api 并获取特定数据?

例如,这是我的 web api "https://api.coinmarketcap.com/v2/ticker/1/",输出是这样的:

{
    "data": {
        "id": 1, 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "website_slug": "bitcoin", 
        "rank": 1, 
        "circulating_supply": 17145625.0, 
        "total_supply": 17145625.0, 
        "max_supply": 21000000.0, 
        "quotes": {
            "USD": {
                "price": 6196.07, 
                "volume_24h": 3623440000.0, 
                "market_cap": 106235492694.0, 
                "percent_change_1h": -0.08, 
                "percent_change_24h": -2.9, 
                "percent_change_7d": -6.64
            }
        }, 
        "last_updated": 1531423345
    }, 
    "metadata": {
        "timestamp": 1531423031, 
        "error": null
    }
}

我想要一个如何打开“web api 链接”、“将 json 导入 web api”、“获取 json 中的数据”的代码。

【问题讨论】:

    标签: json api getjson bitcoin


    【解决方案1】:

    我不太确定您使用的是什么语言。在标签或问题本身中提供该信息会很有帮助

    如果你使用的是 python 3.7

    import json
    from urllib.request import urlopen 
    myjson = JSON.loads(urlopen("https://api.coinmarketcap.com/v2/ticker/1/"))
    

    在这里,您将把 json 反序列化为原生 python 嵌套数组和字典(在您的情况下为字典)。

    从此您可以访问 json 中的元素,就像访问普通 python 字典中的任何元素一样。例如,如果我想访问 id

    myjson["data"]["id"]
    

    希望这有帮助!祝你好运......请回复您使用的语言和任何框架,以便社区可以以最好的方式帮助您。

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 2019-03-30
      • 2023-03-04
      • 2020-06-25
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多