首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP打印JSON对象

使用PHP打印JSON对象
EN

Stack Overflow用户
提问于 2013-06-25 08:54:04
回答 5查看 63.9K关注 0票数 5

我有一个JSON文件,我想在JSON中打印这个对象:

JSON

代码语言:javascript
复制
[{"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)"}]

我试过以下方法,

代码语言:javascript
复制
<?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()

有人能帮我理解我做错了什么吗?

EN

回答 5

Stack Overflow用户

发布于 2016-03-18 13:06:30

代码语言:javascript
复制
<?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对象。

代码语言:javascript
复制
json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
票数 6
EN

Stack Overflow用户

发布于 2013-06-25 08:57:46

使用

$json_output = json_decode($json, true);

默认情况下,json_decode提供对象类型,但您试图以数组的形式访问它,因此传递true将返回一个数组。

阅读文档:http://php.net/manual/en/function.json-decode.php

票数 3
EN

Stack Overflow用户

发布于 2013-06-25 09:02:01

试试下面的代码:

代码语言:javascript
复制
<?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";     
  } 
?>

谢谢你,迪诺

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17293034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档