使用 PHP 打印 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17293034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing JSON object using PHP
提问by user123456789
I have a JSON file and I would like to print that object in JSON:
我有一个 JSON 文件,我想用 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)"}]
I have tried with following method,
我尝试过以下方法,
<?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";
}
?>
but it didn't work:
但它没有用:
Fatal error: Call to undefined function var_dup() in /home/dddd.com/public_html/exp.php on line 5
致命错误:在第 5 行调用 /home/dddd.com/public_html/exp.php 中未定义的函数 var_dup()
Can anyone help me understand what I'm doing wrong?
谁能帮我理解我做错了什么?
回答by Mortgy
<?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;
?>
by using JSON_PRETTY_PRINTu transform your json to pretty formatting, using json_decode($json, true) doesn't reformat your json to PRETTY formatted output, also you don't have to run loop over all keys to export same JSON object again, you could use those constants also which could clean up your json object before exporting it.
通过使用JSON_PRETTY_PRINT将您的 json 转换为漂亮的格式,使用 json_decode($json, true) 不会将您的 json 重新格式化为 PRETTY 格式的输出,您也不必在所有键上运行循环以再次导出相同的 JSON 对象,您也可以使用那些可以在导出 json 对象之前清理它的常量。
json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
回答by Sumit Gupta
use
用
$json_output = json_decode($json, true);
$json_output = json_decode($json, true);
by default json_decode give OBJECT type but you are trying to access it as Array, so passing true will return an array.
默认情况下 json_decode 给出 OBJECT 类型,但您试图将其作为数组访问,因此传递 true 将返回一个数组。
Read documentation : http://php.net/manual/en/function.json-decode.php
回答by dino.keco
Try this code:
试试这个代码:
<?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";
}
?>
Thanks, Dino
谢谢,迪诺
回答by Sandeep Kotian
$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;
This will work.
这将起作用。
回答by Vishal Mhatre
JSON_FORCE_OBJECT
in your json call eg :
JSON_FORCE_OBJECT
在您的 json 调用中,例如:
$obj = json_decode($data);
Instead write like this:
而是这样写:
$obj = json_decode($data, JSON_FORCE_OBJECT);