我的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有效?
答案 0 :(得分:0)
您可以通过这种方式获取hosted_url
(错误:您丢失了数据$json['data']['hosted_url']
)
您还可以检查所需的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']);
答案 1 :(得分: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
答案 2 :(得分: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'];