PHP JSON没有正确解码
我尝试用PHP解码此JSON数据,但它没有返回任何内容:PHP JSON没有正确解码,php,json,Php,Json,我尝试用PHP解码此JSON数据,但它没有返回任何内容: { "message" : "", "result" : [ { "Ask" : 0.040400209999999999, "BaseVolume" : 456.53976963999997, "Bid" : 0.040200010000000001, "Created" : "2014-12-19T03:48:49.13", "High" : 0.0446109999
{ "message" : "",
"result" : [ { "Ask" : 0.040400209999999999,
"BaseVolume" : 456.53976963999997,
"Bid" : 0.040200010000000001,
"Created" : "2014-12-19T03:48:49.13",
"High" : 0.044610999999999998,
"Last" : 0.040400199999999997,
"Low" : 0.037999999999999999,
"MarketName" : "BTC-XPY",
"OpenBuyOrders" : 194,
"OpenSellOrders" : 520,
"PrevDay" : 0.042073039999999999,
"TimeStamp" : "2014-12-30T02:45:32.983",
"Volume" : 11072.491576779999
} ],
"success" : true
}
这就是我到目前为止所做的:
$pricejson = file_get_contents('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-xpy');
$price = json_decode($pricejson, true);
echo $price->result->Last;
当我打开包含此代码的php文件时,什么都没有。如果我使用echo$pricejson
,我会将整个内容打印出来,因此我肯定有数据
怎么了 json_decode的第二个参数强制将所有对象解析为关联数组,因此需要使用数组表示法访问它。此外,
result
始终是一个数组,因此您需要在其上循环或通过索引访问它:
$price = json_decode($pricejson, true);
// print the first price
echo $price['result'][0]['Last'];
// print all prices:
foreach ($price['result'] as $data) {
echo $data['Last'];
}
或者,如果要混合使用对象/数组,则:
$price = json_decode($pricejson);
echo $price->result[0]->Last;
// print all prices:
foreach ($price->result as $data) {
echo $data->Last;
}
除此之外,可能还有一个json解析错误。您可能还需要确保返回的JSON被正确解析。如果您还没有尝试过,1)将
ini\u集(“display\u errors”,TRUE);错误报告(E_全部)编码>在解码之前。2) 重新检查文件@prodigitalson的答案应该会有所帮助,但启用错误报告通常是找到问题原因(从而找到解决方案)的最简单方法。