【问题标题】:Extract data from rome2rio API via JSON file通过 JSON 文件从 rome2rio API 中提取数据
【发布时间】:2014-10-16 17:10:46
【问题描述】:

我正在尝试从 rome2rio (API http://www.rome2rio.com/documentation/search) 中提取数据。他们让我可以从他们服务器上的 JSON 或 XML 文件中获取详细信息。 JSON 输出如下所示:

{
  "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{kl@wb@uVo|AqiDyoBhUibDeiDc`AsmDaxBqk@wwA...",
      "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

	foreach($json as $i){ 
			echo "$i[segments]</br>"; 
			}
 ?>
 
</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

提前感谢您的任何帮助!

【问题讨论】:

    标签: php html json


    【解决方案1】:

    我不知道“段”索引从哪里来。但你可以试试这个:

    <?php foreach($json as $segment => $i):?>
        <h3><?php echo $segment ?></h3>
        <?php foreach($i as $index => $value): ?>
        <p><?php echo $index.": ".$value ?></p>
        <?php endforeach; ?>
    <?php endforeach; ?>
    

    或者你可以使用 Itinaretor...我认为这是一个更好的解决方案..

    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json));
    foreach($iterator as $key => $value) {
        echo "<p>$key => $value</p>";
    }
    

    参考:PHP foreach() with arrays within arrays?

    【讨论】:

    • 有没有可能做某事。像这样? 'code' $iterator = new RecursiveIteratorIterator(new 'code' RecursiveArrayIterator($json)); foreach($iterator as $key => $value) { echo array_search("price",$json,true); } '代码'
    • 如果你想得到某物的价格,请使用: if($key == 'price') echo "Price: ".$value;
    猜你喜欢
    • 2017-04-03
    • 2019-01-22
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多