1
我试图从rome2rio(API http://www.rome2rio.com/documentation/search)中提取数据。他们让我有可能从服务器上的JSON或XML文件中获取详细信息。 JSON的输出是这样的:通过JSON文件从rome2rio API提取数据
{
"agencies":
[{
"code": "SWISSRAILWAYS",
"name": "Swiss Railways (SBB/CFF/FFS)",
"url": "http://www.sbb.ch"
"iconPath": "/logos/trains/ch.png",
"iconSize": "27,23",
"iconOffset": "0,0"
]},
"routes":
[{
"name": "Train",
"distance": 95.92,
"duration": 56,
"stops":
[{
"name": "Bern",
"pos": "46.94926,7.43883",
"kind": "station"
},{
"name": "Zürich HB",
"pos": "47.37819,8.54019",
"kind": "station"
}],
"segments":
[{
"kind": "train",
"subkind": "train",
"isMajor": 1,
"distance": 95.92,
"duration": 56,
"sName": "Bern",
"sPos": "46.94938,7.43927",
"tName": "Zürich HB",
"tPos": "47.37819,8.54019",
"path": "{wp}Gu{[email protected]@uVo|AqiDyoBhUibDeiDc`[email protected]",
"indicativePrice":{
"price":45,
"currency":"USD",
"isFreeTransfer":0,
"nativePrice":40,
"nativeCurrency":"CHF"
},
"itineraries":
[{
"legs":
[{
"url": "http://fahrplan.sbb.ch/bin/query.exe/en...",
"hops":
[{
"distance": 95.92,
"duration": 56,
"sName": "Bern",
"sPos": "46.94938,7.43927",
"tName": "Zürich HB",
"tPos": "47.37819,8.54019",
"frequency": 400,
"indicativePrice":{
"price":45,
"currency":"USD",
"isFreeTransfer":0,
"nativePrice":40,
"nativeCurrency":"CHF"
},
"lines":
[{
"name": "",
"vehicle": "train",
"agency": "SWISSRAILWAYS",
"frequency": 400,
"duration": 57,
}]
}]
}]
}]
}]
}]
}]
}
我想通过PHP提取 '段' 的数据,并使用此代码:
<?php
$url = 'http://free.rome2rio.com/api/1.2/json/Search?key=vwiC3pvW&oName=Germany&dName=Yemen';
$content = file_get_contents($url);
$json = json_decode($content, true);
?>
<html>
<head>
<style>
section {height:500px; width:1000px; color:red;}
</style>
</head>
<body>
<section>
<?php
\t foreach($json as $i){
\t \t \t echo "$i[segments]</br>";
\t \t \t }
?>
</section>
</body>
<
当我执行代码我得到这个结果:
Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21
Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21
Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21
Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21
Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21
Notice: Undefined index: segments in C:\xampp\htdocs\worldmap\test.php on line 21
预先感谢任何形式的帮助!
谢谢! '迭代器'工作得很好。 – Rezin 2014-10-16 17:54:44
有没有可能做s.th.喜欢这个? 'code' $ iterator = new RecursiveIteratorIterator(new'code' RecursiveArrayIterator($ json)); \t foreach($ iterator as $ key => $ value){ \t \t echo array_search(“price”,$ json,true); } 'code' – Rezin 2014-10-16 18:46:45
如果你想得到东西的价格使用这个:if($ key =='price')echo“Price:”。$ value; – giordanolima 2014-10-16 18:54:57