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 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备