JSON只在解码时返回第一个字符?

My 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"}]}}

My PHP:

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

        echo $json['hosted_url'];

It just returns { always, in fact even if I put $json['safasfsaf'] it would still return {

What is the issue, the JSON is valid?

You can get hosted_url in this way (ERROR: you missing data $json['data']['hosted_url'])

also you can check your desired output here

<?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']);

You have error reporting turned off

This error is hidden for you

Warning: Illegal string offset 'hosted_url'

You can turn error reporting on with this code

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

For your code you need to replace $json['hosted_url'] with $json['data']['hosted_url']

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

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

Also response is already json so you must not json_encode it

I have noticed that, you are decode json data two time, so you are getting errors.

Hi, i have tried this way.

$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'];