我有一个JSON文件,我想在JSON中打印这个对象:
JSON
[{"text": "Aachen, Germany - Aachen/Merzbruck (AAH)"}, {"text": "Aachen, Germany - Railway (ZIU)"}, {"text": "Aalborg, Denmark - Aalborg (AAL)"}, {"text": "Aalesund, Norway - Vigra (AES)"}, {"text": "Aarhus, Denmark - Aarhus Airport (AAR)"}, {"text": "Aarhus Limo, Denmark - Aarhus Limo (ZBU)"}, {"text": "Aasiaat, Greenland - Aasiaat (JEG)"}, {"text": "Abadan, Iran - Abadan (ABD)"}]我试过以下方法,
<?php
$jsonurl='http://website.com/international.json';
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ($json_output as $trend)
{
echo "{$trend->text}\n";
}
?>但没有用:
致命错误:在第5行调用/home/dd.com/public_html/exp.php中的未定义函数var_dup()
有人能帮我理解我做错了什么吗?
发布于 2016-03-18 13:06:30
<?php
$jsonurl='http://website.com/international.json';
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, JSON_PRETTY_PRINT);
echo $json_output;
?>通过使用打印 u将您的json转换为相当格式,使用json_decode($json,true)不会将json格式重新格式化为格式化良好的输出,您也不必在所有键上运行循环才能再次导出相同的JSON对象,您也可以使用这些常量在导出json对象之前清理json对象。
json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)发布于 2013-06-25 08:57:46
使用
$json_output = json_decode($json, true);
默认情况下,json_decode提供对象类型,但您试图以数组的形式访问它,因此传递true将返回一个数组。
阅读文档:http://php.net/manual/en/function.json-decode.php
发布于 2013-06-25 09:02:01
试试下面的代码:
<?php
$jsonurl='http://website.com/international.json';
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, true);
foreach ($json_output as $trend){
echo $trend['text']."\n";
}
?>谢谢你,迪诺
https://stackoverflow.com/questions/17293034
复制相似问题