我正试图从API获得以下JSON响应中的某些信息。这是实际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
}]
}
我想要获得naptanId,行标识符和指向作为名称值对,并打印它们。例如,
请帮帮我,我是初学者,谢谢
发布于 2016-04-08 17:46:03
根据您的问题,您似乎只对返回的第一个place
的值感兴趣,所以我将在答案中假设这一点。
要将数据从API中获取到对象结构中(与最初获得的JSON字符串相反):
$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
$naptanId = $data['places'][0]['naptanId'];
要获得lineIdentifier
,我假设它来自第一个lineGroup
元素(不是lineModeGroups
,但可以酌情更改):
$lineIdentifier = $data['places'][0]['lineGroup'][0]['lineIdentifier'];
此值将是一个数组。
要获得towards
值要复杂一些,因为您需要根据子键过滤additionalProperties
数组,然后取出value
;同样,我假设您只对第一个值感兴趣:
$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]
时它将失败。如果您需要处理这些情况(您几乎肯定会这样做),那么您需要预先检查这些情况,例如:
if (!isset($data['places']) || sizeof($data['places']) === 0) {...}
https://stackoverflow.com/questions/36503939
复制相似问题