dongyun6835 2016-04-08 15:46
浏览 50
已采纳

使用PHP从TFL api处理JSON响应?

I am trying to get certain information from the following JSON response from an API. This is the actual API call.

{
  "$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
  }]
}

I want to get the naptanId, line identifier and towards as name value pairs and print them. For example,

  • naptan :490009219W;
  • lineidentifer Whatever the values are
  • towards : "Eastham or manor park"

Please help me I am a beginner, Thank you in advance

</div>
  • 写回答

1条回答 默认 最新

  • douren5490 2016-04-08 17:46
    关注

    Based on your question, it seems you are only interested in the values from the first place returned, so I'll assume that in the answer.

    To get the data from the API into an object structure (as opposed to the JSON string you initially get):

    $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);
    

    To get the naptanId:

    $naptanId = $data['places'][0]['naptanId'];
    

    To get the lineIdentifier, which I'll assume is the one from the first lineGroup element (not lineModeGroups, but you can change as appropriate):

    $lineIdentifier = $data['places'][0]['lineGroup'][0]['lineIdentifier'];
    

    This value will be an array.

    To get the towards value is a bit more complex, because you need to filter the additionalProperties array based on a child key, then pull out the value; again I'm assuming you're only interested in the first value:

    $towards = array_values(array_filter(
        $data['places'][0]['additionalProperties'], 
        function ($property) {
            return $property['key'] === 'Towards';
        }
    ))[0]['value'];
    

    The call to array_filter pulls out the additionalProperties element where key === 'Towards' (note this assumes case sensitivity, you could add a strtolower and change the string literal to 'towards' if you want to match case insensitively). The call to array_values is necessary to normalise the array indexes. Then we pull out the first element [0] and get its value property.

    Note that the above code does not do any error checking, for example if the API call does not return any places then the first time you do $data['places'][0] it will fail. If you need to handle these cases (which you almost certainly do), then you will need to pre-check these things, e.g.:

    if (!isset($data['places']) || sizeof($data['places']) === 0) {...}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)