【问题标题】:Extract and Display a Value from a JSON Array从 JSON 数组中提取并显示一个值
【发布时间】:2016-01-25 13:55:09
【问题描述】:

我正在使用 Coindesk JSON API 获取最新的比特币汇率。

http://api.coindesk.com/v1/bpi/currentprice/GBP.json

我想在我的网站页面上以纯文本形式显示汇率,例如:“284.0493”。从 JSON 数组中提取正确值并将其显示在 HTML div 中的正确方法是什么?

任何使用 JavaScript、PHP 甚至 Python 的解决方案都可以。

到目前为止,我一直在 this 工作。

谢谢!

【问题讨论】:

  • 具体是什么问题?您用来检索数据的代码在哪里?展示您尝试过的内容
  • echo json_decode(file_get_contents($url))->bpi->USD->rate;

标签: javascript php jquery python json


【解决方案1】:

首先,有问题的json数据不是数组而是对象!

可视化任何一段 json 数据的简单方法是这样做

<?php
$j = '{"time":{"updated":"Oct 26, 2015 12:54:00 UTC","updatedISO":"2015-10-26T12:54:00+00:00","updateduk":"Oct 26, 2015 at 12:54 GMT"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","bpi":{"USD":{"code":"USD","rate":"283.9203","description":"United States Dollar","rate_float":283.9203},"GBP":{"code":"GBP","rate":"185.0732","description":"British Pound Sterling","rate_float":185.0732}}}';

print_r( json_decode($j) );

在这种情况下会产生:-

stdClass Object
(
    [time] => stdClass Object
        (
            [updated] => Oct 26, 2015 12:54:00 UTC
            [updatedISO] => 2015-10-26T12:54:00+00:00
            [updateduk] => Oct 26, 2015 at 12:54 GMT
        )

    [disclaimer] => This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchang
erates.org
    [bpi] => stdClass Object
        (
            [USD] => stdClass Object
                (
                    [code] => USD
                    [rate] => 283.9203
                    [description] => United States Dollar
                    [rate_float] => 283.9203
                )

            [GBP] => stdClass Object
                (
                    [code] => GBP
                    [rate] => 185.0732
                    [description] => British Pound Sterling
                    [rate_float] => 185.0732
                )

        )

)

这很简单地告诉您a)它是一个对象,其属性也是对象等,以及b)如何引用对象中的任何属性。

所以要得到你想要的汇率,我会这样做:-

<?php
$j = '{"time":{"updated":"Oct 26, 2015 12:54:00 UTC","updatedISO":"2015-10-26T12:54:00+00:00","updateduk":"Oct 26, 2015 at 12:54 GMT"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","bpi":{"USD":{"code":"USD","rate":"283.9203","description":"United States Dollar","rate_float":283.9203},"GBP":{"code":"GBP","rate":"185.0732","description":"British Pound Sterling","rate_float":185.0732}}}';

$bitExchange = json_decode($j);

// the GBP exchange rate will be
echo $bitExchange->bpi->GBP->rate;
// or if you prefer
echo $bitExchange->bpi->GBP->rate_float;

【讨论】:

    【解决方案2】:

    假设json数据在python中存储在json_text,可以这样做

    import json
    json.loads(json_text).get('bpi').get('USD').get('rate')
    

    如果数据对您很重要,您应该检查错误。此外,使货币可配置可能是一个好主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2020-10-06
      • 2022-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多