【问题标题】:Printing JSON object using PHP使用 PHP 打印 JSON 对象
【发布时间】:2013-06-21 23:54:41
【问题描述】:

我有一个 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/dddd.com/public_html/exp.php 中未定义的函数 var_dup()

谁能帮我理解我做错了什么?

【问题讨论】:

标签: php json


【解决方案1】:
<?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;
?>

通过使用JSON_PRETTY_PRINT u 将您的 json 转换为漂亮的格式,使用 json_decode($json, true) 不会将您的 json 重新格式化为 PRETTY 格式的输出,您也不必遍历所有键来导出相同的再次使用 JSON 对象,您也可以使用这些常量,这些常量也可以在导出之前清理您的 json 对象。

json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)

【讨论】:

  • 虽然此答案可能是正确且有用的,但如果您 include some explanation along with it 解释它如何帮助解决问题,则最好。如果有变化(可能不相关)导致它停止工作并且用户需要了解它曾经是如何工作的,这在未来变得特别有用。谢谢!
  • 感谢您的评论,我会用更多解释更新我的答案:)
  • @Mortgy 这不能直接与 json_decode 一起使用,你需要这样的东西:json_encode(json_decode($json_ugly), JSON_PRETTY_PRINT); 取自:link
【解决方案2】:

使用

$json_output = json_decode($json, true);

默认情况下 json_decode 给出 OBJECT 类型,但您尝试将其作为数组访问,因此传递 true 将返回一个数组。

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

【讨论】:

    【解决方案3】:

    试试这个代码:

    <?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";     
      } 
    ?>
    

    谢谢, 迪诺

    【讨论】:

    • 警告:在第 6 行的 /home/website/public_html/exp.php 中为 foreach() 提供的参数无效
    • 从该 URL 下载的 JSON 存在问题。请仔细检查 JSON 是否有效。
    • 这段代码没有问题:
    • 非常感谢!干得漂亮!
    【解决方案4】:
    $data=[{"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)"}]
    $obj = json_decode($data);
    $text = $obj[0]->text;
    

    这会起作用。

    【讨论】:

      【解决方案5】:

      JSON_FORCE_OBJECT 在您的 json 调用中,例如:

      $obj = json_decode($data);
      

      改为这样写:

      $obj = json_decode($data, JSON_FORCE_OBJECT);
      

      【讨论】:

      • 嗨,维沙尔。我已对您的答案进行了建议的编辑以格式化代码。如果可能的话,您能否提供有关修复工作的详细信息?谢谢!
      猜你喜欢
      • 2013-10-20
      • 2021-10-15
      • 2021-04-16
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 2011-08-13
      • 2014-12-29
      相关资源
      最近更新 更多