首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PHP处理来自TFL的JSON响应?

使用PHP处理来自TFL的JSON响应?
EN

Stack Overflow用户
提问于 2016-04-08 15:46:43
回答 1查看 273关注 0票数 0

我正试图从API获得以下JSON响应中的某些信息。这是实际API call。

代码语言:javascript
运行
复制
{
  "$type": "Tfl.Api.Presentation.Entities.PlacesResponse, Tfl.Api.Presentation.Entities",
  "centrePoint": [
    51.555,
    0.059
  ],
  "places": [{
    "$type": "Tfl.Api.Presentation.Entities.StopPoint, Tfl.Api.Presentation.Entities",
    "naptanId": "490009219W",
    "indicator": "Stop B",
    "stopLetter": "B",
    "modes": [
      "bus"
    ],
    "icsCode": "1009219",
    "stopType": "NaptanPublicBusCoachTram",
    "stationNaptan": "490G00009219",
    "lines": [{
      "$type": "Tfl.Api.Presentation.Entities.Identifier, Tfl.Api.Presentation.Entities",
      "id": "25",
      "name": "25",
      "uri": "/Line/25",
      "type": "Line"
    }, {
      "$type": "Tfl.Api.Presentation.Entities.Identifier, Tfl.Api.Presentation.Entities",
      "id": "86",
      "name": "86",
      "uri": "/Line/86",
      "type": "Line"
    }, {
      "$type": "Tfl.Api.Presentation.Entities.Identifier, Tfl.Api.Presentation.Entities",
      "id": "w19",
      "name": "W19",
      "uri": "/Line/w19",
      "type": "Line"
    }],
    "lineGroup": [{
      "$type": "Tfl.Api.Presentation.Entities.LineGroup, Tfl.Api.Presentation.Entities",
      "naptanIdReference": "490009219W",
      "stationAtcoCode": "490G00009219",
      "lineIdentifier": [
        "25",
        "86",
        "w19"
      ]
    }],
    "lineModeGroups": [{
      "$type": "Tfl.Api.Presentation.Entities.LineModeGroup, Tfl.Api.Presentation.Entities",
      "modeName": "bus",
      "lineIdentifier": [
        "25",
        "86",
        "w19"
      ]
    }],
    "status": true,
    "id": "490009219W",
    "commonName": "Little Ilford Lane",
    "distance": 64.10041498232529,
    "placeType": "StopPoint",
    "additionalProperties": [{
      "$type": "Tfl.Api.Presentation.Entities.AdditionalProperties, Tfl.Api.Presentation.Entities",
      "category": "Direction",
      "key": "CompassPoint",
      "sourceSystemKey": "Naptan490",
      "value": "W"
    }, {
      "$type": "Tfl.Api.Presentation.Entities.AdditionalProperties, Tfl.Api.Presentation.Entities",
      "category": "Direction",
      "key": "Towards",
      "sourceSystemKey": "CountDown",
      "value": "East Ham or Manor Park"
    }],
    "lat": 51.554475,
    "lon": 0.059381
  }]
}

我想要获得naptanId,行标识符和指向作为名称值对,并打印它们。例如,

  • 凝固剂:490009219W;
  • 行标识符,不管这些值是什么
  • 朝向:"Eastham or manor park“

请帮帮我,我是初学者,谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-08 17:46:03

根据您的问题,您似乎只对返回的第一个place的值感兴趣,所以我将在答案中假设这一点。

要将数据从API中获取到对象结构中(与最初获得的JSON字符串相反):

代码语言:javascript
运行
复制
$data = json_decode(file_get_contents('https://api.tfl.gov.uk/Place?lat=51.555504&lon=0.0592359&radius=200&includeChildren=False&type=NaptanPublicBusCoachTram&app_id=&app_key='), true);

以获得naptanId

代码语言:javascript
运行
复制
$naptanId = $data['places'][0]['naptanId'];

要获得lineIdentifier,我假设它来自第一个lineGroup元素(不是lineModeGroups,但可以酌情更改):

代码语言:javascript
运行
复制
$lineIdentifier = $data['places'][0]['lineGroup'][0]['lineIdentifier'];

此值将是一个数组。

要获得towards值要复杂一些,因为您需要根据子键过滤additionalProperties数组,然后取出value;同样,我假设您只对第一个值感兴趣:

代码语言:javascript
运行
复制
$towards = array_values(array_filter(
    $data['places'][0]['additionalProperties'], 
    function ($property) {
        return $property['key'] === 'Towards';
    }
))[0]['value'];

array_filter的调用提取了additionalProperties元素key === 'Towards' (注意,这假设大小写敏感,如果您想不敏感地匹配大小写,可以添加strtolower并将字符串文本更改为'towards' )。对array_values的调用对于规范数组索引是必要的。然后,我们提取第一个元素[0]并得到它的value属性。

请注意,上面的代码不执行任何错误检查,例如,如果API调用不返回任何places,那么第一次执行$data['places'][0]时它将失败。如果您需要处理这些情况(您几乎肯定会这样做),那么您需要预先检查这些情况,例如:

代码语言:javascript
运行
复制
if (!isset($data['places']) || sizeof($data['places']) === 0) {...}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36503939

复制
相关文章

相似问题

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