Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP处理来自TFLAPI的JSON响应?_Php_Json - Fatal编程技术网

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

使用PHP处理来自TFLAPI的JSON响应?,php,json,Php,Json,我试图从API的以下JSON响应中获得某些信息。这是l { “$type”:“Tfl.Api.Presentation.Entities.PlacesResponse,Tfl.Api.Presentation.Entities”, “中心点”:[ 51.555, 0.059 ], “地点”:[{ “$type”:“Tfl.Api.Presentation.Entities.StopPoint,Tfl.Api.Presentation.Entities”, “naptanId”:“4900921

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

{
“$type”:“Tfl.Api.Presentation.Entities.PlacesResponse,Tfl.Api.Presentation.Entities”,
“中心点”:[
51.555,
0.059
],
“地点”:[{
“$type”:“Tfl.Api.Presentation.Entities.StopPoint,Tfl.Api.Presentation.Entities”,
“naptanId”:“49009219W”,
“指示器”:“停止B”,
“stopLetter”:“B”,
“模式”:[
“公共汽车”
],
“icsCode”:“1009219”,
“stopType”:“NaptanPublicBusCoachTram”,
“stationNaptan”:“490G00009219”,
“行”:[{
“$type”:“Tfl.Api.Presentation.Entities.Identifier,Tfl.Api.Presentation.Entities”,
“id”:“25”,
“姓名”:“25”,
“uri”:“/行/25”,
“类型”:“行”
}, {
“$type”:“Tfl.Api.Presentation.Entities.Identifier,Tfl.Api.Presentation.Entities”,
“id”:“86”,
“姓名”:“86”,
“uri”:“/行/86”,
“类型”:“行”
}, {
“$type”:“Tfl.Api.Presentation.Entities.Identifier,Tfl.Api.Presentation.Entities”,
“id”:“w19”,
“名称”:“W19”,
“uri”:“/行/w19”,
“类型”:“行”
}],
“线组”:[{
“$type”:“Tfl.Api.Presentation.Entities.LineGroup,Tfl.Api.Presentation.Entities”,
“naptanIdReference”:“49009219W”,
“StationCocode”:“490G00009219”,
“行标识符”:[
"25",
"86",
“w19”
]
}],
“lineModeGroups”:[{
“$type”:“Tfl.Api.Presentation.Entities.LineModeGroup,Tfl.Api.Presentation.Entities”,
“modeName”:“总线”,
“行标识符”:[
"25",
"86",
“w19”
]
}],
“状态”:正确,
“id”:“49009219W”,
“commonName”:“小伊尔福德巷”,
“距离”:64.10041498232529,
“地点类型”:“停止点”,
“附加属性”:[{
“$type”:“Tfl.Api.Presentation.Entities.AdditionalProperties,Tfl.Api.Presentation.Entities”,
“类别”:“方向”,
“密钥”:“CompassPoint”,
“sourceSystemKey”:“Naptan490”,
“值”:“W”
}, {
“$type”:“Tfl.Api.Presentation.Entities.AdditionalProperties,Tfl.Api.Presentation.Entities”,
“类别”:“方向”,
“钥匙”:“朝向”,
“sourceSystemKey”:“倒计时”,
“价值”:“东哈姆或庄园公园”
}],
“lat”:51.554475,
“lon”:0.059381
}]

}
根据您的问题,您似乎只对返回的第一个
位置的值感兴趣,因此我将在回答中假设这一点

要将数据从API获取到对象结构中(与最初获取的JSON字符串相反),请执行以下操作:

要获取
naptanId

$naptanId = $data['places'][0]['naptanId'];
要获取
lineIdentifier
,我将假定它是来自第一个
lineGroup
元素的标识符(不是
lineModeGroups
,但您可以根据需要进行更改):

此值将是一个数组

要获得
朝向
的值要复杂一些,因为需要根据子键过滤
附加属性
数组,然后拉出
;我再次假设您只对第一个值感兴趣:

$towards = array_values(array_filter(
    $data['places'][0]['additionalProperties'], 
    function ($property) {
        return $property['key'] === 'Towards';
    }
))[0]['value'];
array\u filter
的调用将拉出
additionalProperties
元素,其中
键===='朝向'
(注意,这假设区分大小写,如果希望不区分大小写地匹配,可以添加一个
strtolower
,并将字符串文本更改为
'朝向'
)。调用
array\u values
是使数组索引正常化所必需的。然后我们取出第一个元素
[0]
,并得到它的
属性

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

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