Laravel - 从 Foursquare API 获取并解析'Complex' JSON 响应



我在Laravel控制器中解析JSON响应时遇到困难。它可以很好地处理"简单"响应,但这一个有很多嵌套数组,所以我很困惑应该如何处理

到目前为止,我的控制器中有以下内容:

public function foursquare()
{
$response = Http::get('https://api.foursquare.com/v2/venues/explore', [
'client_id' => 'XYZXYZ',
'client_secret' => 'XYZXYZ',
'limit' => 10,
'v' => 20210404,
'near' => 'Paris',
]);
$foursquare = json_decode($response->body());
foreach($venues as $venue){}

尽管我不知道在前臂环里放什么。我想提取所有的场地名称,比如下面的例子:"Parc des Buttes Chaumont"one_answers"Grand Bassin du Jardin du Luxembourg"。我该怎么做?

这是的样本响应

{
"meta": {
"code": 200,
"requestId": "606a7fe84ba45b203bad5404"
},
"response": {
"suggestedFilters": {
"header": "Tap to show:",
"filters": [
{
"name": "Open now",
"key": "openNow"
}
]
},
"geocode": {
"what": "",
"where": "paris",
"center": {
"lat": 48.85341,
"lng": 2.3488
},
"displayString": "Paris, France",
"cc": "FR",
"geometry": {
"bounds": {
"ne": {
"lat": 49.03356694704047,
"lng": 2.6042489194755207
},
"sw": {
"lat": 48.670322030624696,
"lng": 2.1084049608796893
}
}
},
"slug": "paris",
"longId": "72057594040916443"
},
"headerLocation": "Paris",
"headerFullLocation": "Paris",
"headerLocationGranularity": "city",
"totalResults": 162,
"suggestedBounds": {
"ne": {
"lat": 48.881521148542696,
"lng": 2.3847928047180176
},
"sw": {
"lat": 48.84525583122255,
"lng": 2.323707103729248
}
},
"groups": [
{
"type": "Recommended Places",
"name": "recommended",
"items": [
{
"reasons": {
"count": 0,
"items": [
{
"summary": "This spot is popular",
"type": "general",
"reasonName": "globalInteractionReason"
}
]
},
"venue": {
"id": "4adcda09f964a520113421e3",
"name": "Parc des Buttes-Chaumont",
"location": {
"address": "Rue Botzaris",
"crossStreet": "Rue de Crimée",
"lat": 48.87987272502814,
"lng": 2.382016181945801,
"labeledLatLngs": [
{
"label": "display",
"lat": 48.87987272502814,
"lng": 2.382016181945801
}
],
"postalCode": "75019",
"cc": "FR",
"neighborhood": "Buttes-Chaumont, Paris",
"city": "Paris",
"state": "Île-de-France",
"country": "France",
"formattedAddress": [
"Rue Botzaris (Rue de Crimée)",
"75019 Paris",
"France"
]
},
"categories": [
{
"id": "4bf58dd8d48988d163941735",
"name": "Park",
"pluralName": "Parks",
"shortName": "Park",
"icon": {
"prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/park_",
"suffix": ".png"
},
"primary": true
}
],
"photos": {
"count": 0,
"groups": []
}
},
"referralId": "e-0-4adcda09f964a520113421e3-0"
},
{
"reasons": {
"count": 0,
"items": [
{
"summary": "This spot is popular",
"type": "general",
"reasonName": "globalInteractionReason"
}
]
},
"venue": {
"id": "55536fca498e1bf662c2ed7d",
"name": "Grand Bassin du Jardin du Luxembourg",
"location": {
"address": "Jardin du Luxembourg",
"lat": 48.8469042547371,
"lng": 2.3371830582618713,
"labeledLatLngs": [
{
"label": "display",
"lat": 48.8469042547371,
"lng": 2.3371830582618713
}
],
"postalCode": "75006",
"cc": "FR",
"neighborhood": "Odéon",
"city": "Paris",
"state": "Île-de-France",
"country": "France",
"formattedAddress": [
"Jardin du Luxembourg",
"75006 Paris",
"France"
]
},
"categories": [
{
"id": "56aa371be4b08b9a8d573547",
"name": "Fountain",
"pluralName": "Fountains",
"shortName": "Fountain",
"icon": {
"prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/plaza_",
"suffix": ".png"
},
"primary": true
}
],
"photos": {
"count": 0,
"groups": []
}
},
"referralId": "e-0-55536fca498e1bf662c2ed7d-1"
},
{
"reasons": {
"count": 0,
"items": [
{
"summary": "This spot is popular",
"type": "general",
"reasonName": "globalInteractionReason"
}
]
},

然后,您应该循环通过$response['response']['groups']。在您的响应中,项目有一个对象数组,在循环中访问它,就像这个

$i = 0;
foreach($response['response']['groups'] as $key => $value) 
{
echo $value['items']['venue']['name'];
}

您可以尝试使用此代码,这可能会给您带来一些想法。

最新更新