-1

我的 JSON:

{"data":{"addresses":{"bitcoincash":"qzx3k8cq2e66k4glnt2derr5mppzc6xmvuxgsyp778","bitcoin":"1GjKuo1Q9sw8NytE31J5RPnVpYpEzp47hu","ethereum":"0xd7410e84e9c336937637e3cb472ad112c258ede3","litecoin":"LiQCBwuvW4RVuAg2dBNzS4fkviDwi8EBKa"},"code":"PGVD745Y","created_at":"2018-08-18T04:26:23Z","description":"dddd","expires_at":"2018-08-18T05:26:23Z","hosted_url":"https://commerce.coinbase.com/charges/example","id":"ddd","metadata":{"customer_id":"IuYBD5X7ylEV6g0xyTWi","name":"Guest@localhost.com"},"name":"ddd","payments":[],"pricing":{"local":{"amount":"19.85","currency":"USD"},"ethereum":{"amount":"0.063584000","currency":"ETH"},"bitcoin":{"amount":"0.00303719","currency":"BTC"},"bitcoincash":{"amount":"0.03345637","currency":"BCH"},"litecoin":{"amount":"0.32861518","currency":"LTC"}},"pricing_type":"fixed_price","resource":"charge","timeline":[{"status":"NEW","time":"2018-08-18T04:26:23Z"}]}}

我的PHP:

$exec = json_encode($exec);
        $json = json_decode($exec, TRUE);

        echo $json['hosted_url'];

{总是返回,事实上即使我把$json['safasfsaf']它仍然会返回{

有什么问题,JSON是有效的?

4

3 回答 3

0

您可以通过hosted_url这种方式获得(错误:您缺少数据$json['data']['hosted_url']

你也可以在这里检查你想要的输出

<?php
$a = '{
    "data": {
        "addresses": {
            "bitcoincash": "qzx3k8cq2e66k4glnt2derr5mppzc6xmvuxgsyp778",
            "bitcoin": "1GjKuo1Q9sw8NytE31J5RPnVpYpEzp47hu",
            "ethereum": "0xd7410e84e9c336937637e3cb472ad112c258ede3",
            "litecoin": "LiQCBwuvW4RVuAg2dBNzS4fkviDwi8EBKa"
        },
        "code": "PGVD745Y",
        "created_at": "2018-08-18T04:26:23Z",
        "description": "dddd",
        "expires_at": "2018-08-18T05:26:23Z",
        "hosted_url": "https://commerce.coinbase.com/charges/example",
        "id": "ddd",
        "metadata": {
            "customer_id": "IuYBD5X7ylEV6g0xyTWi",
            "name": "Guest@localhost.com"
        },
        "name": "ddd",
        "payments": [],
        "pricing": {
            "local": {
                "amount": "19.85",
                "currency": "USD"
            },
            "ethereum": {
                "amount": "0.063584000",
                "currency": "ETH"
            },
            "bitcoin": {
                "amount": "0.00303719",
                "currency": "BTC"
            },
            "bitcoincash": {
                "amount": "0.03345637",
                "currency": "BCH"
            },
            "litecoin": {
                "amount": "0.32861518",
                "currency": "LTC"
            }
        },
        "pricing_type": "fixed_price",
        "resource": "charge",
        "timeline": [{
            "status": "NEW",
            "time": "2018-08-18T04:26:23Z"
        }]
    }
}';

$json = json_decode($a, TRUE);
echo "<pre>";
print_r($json['data']['hosted_url']);
于 2018-08-18T04:35:05.547 回答
0

您已关闭错误报告

此错误已为您隐藏

警告:非法字符串偏移 'hosted_url'

您可以使用此代码打开错误报告

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

对于您的代码,您需要替换$json['hosted_url']$json['data']['hosted_url']

$exec = getJson();
$json = json_decode($exec, TRUE);

echo $json['data']['hosted_url'];

响应也已经是 json 所以你一定不要json_encode

于 2018-08-18T04:36:33.713 回答
0

我注意到,你两次解码 json 数据,所以你得到了错误。

你好,我试过这种方法。

$exe = '{"data":{"addresses":{"bitcoincash":"qzx3k8cq2e66k4glnt2derr5mppzc6xmvuxgsyp778","bitcoin":"1GjKuo1Q9sw8NytE31J5RPnVpYpEzp47hu","ethereum":"0xd7410e84e9c336937637e3cb472ad112c258ede3","litecoin":"LiQCBwuvW4RVuAg2dBNzS4fkviDwi8EBKa"},"code":"PGVD745Y","created_at":"2018-08-18T04:26:23Z","description":"dddd","expires_at":"2018-08-18T05:26:23Z","hosted_url":"https://commerce.coinbase.com/charges/example","id":"ddd","metadata":{"customer_id":"IuYBD5X7ylEV6g0xyTWi","name":"Guest@localhost.com"},"name":"ddd","payments":[],"pricing":{"local":{"amount":"19.85","currency":"USD"},"ethereum":{"amount":"0.063584000","currency":"ETH"},"bitcoin":{"amount":"0.00303719","currency":"BTC"},"bitcoincash":{"amount":"0.03345637","currency":"BCH"},"litecoin":{"amount":"0.32861518","currency":"LTC"}},"pricing_type":"fixed_price","resource":"charge","timeline":[{"status":"NEW","time":"2018-08-18T04:26:23Z"}]}}';
$data = json_decode($exe, TRUE);
echo $data['data']['hosted_url'];
于 2018-08-18T05:20:07.457 回答