这是一个一般参考问题和答案,涵盖了许多永无止境的"如何访问我的JSON中的数据?" 的问题.它可以处理在PHP中解码JSON和访问结果的广泛基础知识.
我有JSON:
{
"type": "donut",
"name": "Cake",
"toppings": [
{ "id": "5002", "type": "Glazed" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5004", "type": "Maple" }
]
}
Run Code Online (Sandbox Code Playgroud)
如何在PHP中解码并访问结果数据?
我在这里有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)"}]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法,
<?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";
}
?>
Run Code Online (Sandbox Code Playgroud)
但这没有用,谁能帮助我了解我在做什么错。