为什么我无法使用PHP获取JSON数据? [英] Why am I unable to get JSON data using PHP?
问题描述
这是JSON文件:
//[{"id":"5417778","t":"TATAMTRDVR","e":"NSE","l": "329.80","l_fix":"329.80","l_cur":"Rs.329.80","s":"0" ,"ltt":"11:11 AM GMT + 5:30","lt":"Jul 26,11:11 AM GMT + 5:30","lt_dts" :"2016-07-26T11:11:45Z","c":"-0.35","c_fix":"-0.35","cp": "-0.11","cp_fix":"-0.11","ccol":"chr","pcls_fix":"330.15"}]
// [ { "id": "5417778" ,"t" : "TATAMTRDVR" ,"e" : "NSE" ,"l" : "329.80" ,"l_fix" : "329.80" ,"l_cur" : "Rs.329.80" ,"s": "0" ,"ltt":"11:11AM GMT+5:30" ,"lt" : "Jul 26, 11:11AM GMT+5:30" ,"lt_dts" : "2016-07-26T11:11:45Z" ,"c" : "-0.35" ,"c_fix" : "-0.35" ,"cp" : "-0.11" ,"cp_fix" : "-0.11" ,"ccol" : "chr" ,"pcls_fix" : "330.15" } ]
我的代码是
<?php
$json = file_get_contents('http://finance.google.com/finance/info?q=NSE:TATAMTRDVR');
$obj = json_decode($json);
echo $obj->id;
?>
显示的错误消息是
注意:尝试在第4行的C:\ xampp \ htdocs \ fin \ latest_stock.php中获取非对象的属性
Notice: Trying to get property of non-object in C:\xampp\htdocs\fin\latest_stock.php on line 4
推荐答案
它不是有效的json.从json响应中删除"//",您的代码将正常工作.
It is not a valid json. Remove "// " from json response and your code will work fine.
这篇关于为什么我无法使用PHP获取JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!