首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过rome2rio文件从JSON中提取数据

通过rome2rio文件从JSON中提取数据
EN

Stack Overflow用户
提问于 2014-10-17 01:10:47
回答 1查看 957关注 0票数 1

我正在尝试从rome2rio (http://www.rome2rio.com/documentation/search接口)中提取数据。它们为我提供了从服务器上的JSON或XML文件中获取详细信息的可能性。

代码语言:javascript
复制
{
  "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提取“段”数据,并使用以下代码:

代码语言:javascript
复制
<?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>
<

当我执行代码时,我得到以下结果:

代码语言:javascript
复制
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

提前感谢您的任何帮助!

EN

回答 1

Stack Overflow用户

发布于 2014-10-17 01:17:55

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

代码语言:javascript
复制
<?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..。我认为这是一个更好的解决方案..

代码语言:javascript
复制
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json));
foreach($iterator as $key => $value) {
    echo "<p>$key => $value</p>";
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26410124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档