我尝试解析this json file以获取使用PHP列出的所有路线值:
我不知道为什么我的代码会起作用:
$json_source = file_get_contents('https://pastebin.com/raw/TTuV7K7J');
$data = json_decode($json_source);
foreach ($data->departures as $a) {
foreach ($a->display_informations as $b) {
echo $b->direction . '<br />';
}
}
我一直有这个错误:
尝试获取非对象的属性
echo
是导致代码崩溃的行。当我显示var_dump($b)
时,我为每个JSON条目获得了以下内容:
C:\wamp64\www\index.php:10:string 'Versailles-Chantiers (Versailles)' (length=33)
C:\wamp64\www\index.php:10:string 'N' (length=1)
C:\wamp64\www\index.php:10:string 'SNCF' (length=4)
C:\wamp64\www\index.php:10:
array (size=0)
empty
C:\wamp64\www\index.php:10:string '00A092' (length=6)
C:\wamp64\www\index.php:10:string 'Paris-Montparnasse 1-2 - Versailles-Chantiers' (length=45)
C:\wamp64\www\index.php:10:string 'Train de banlieue / RER' (length=23)
C:\wamp64\www\index.php:10:string '164273' (length=6)
C:\wamp64\www\index.php:10:string 'N' (length=1)
C:\wamp64\www\index.php:10:
array (size=0)
empty
C:\wamp64\www\index.php:10:string '' (length=0)
C:\wamp64\www\index.php:10:string 'Transilien' (length=10)
C:\wamp64\www\index.php:10:string '' (length=0)
以下是json的样子:
{
"pagination":{},
"links":[],
"disruptions":[],
"notes":[],
"feed_publishers":[],
"departures":[
{
"display_informations":{
"direction":"Versailles-Chantiers (Versailles)",
"code":"N",
"network":"SNCF",
"links":[],
"color":"00A092",
"name":"Paris-Montparnasse 1-2 - Versailles-Chantiers",
"physical_mode":"Train de banlieue \/ RER",
"headsign":"164273",
"label":"N",
"equipments":[],
"text_color":"",
"commercial_mode":"Transilien",
"description":""
},
"stop_point":{},
"route":{},
"links":[],
"stop_date_time":{}
},
{...},
{...},
],
"context":{},
"exceptions":[]
}
我应该如何获得direction
值?