首页 > 解决方案 > Laravel - 从 Foursquare API 获取和解析“复杂”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){}

虽然我不知道在我的 foreach 循环中放什么。我希望提取所有场地名称,例如在下面的示例中:“Parc des Buttes-Chaumont”和“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"
                            }
                        ]
                    },

标签: jsonlaravellaravel-6jsondecoder

解决方案


然后,您应该循环遍历$response['response']['groups']. 在您的回复中,项目有对象数组,在循环中访问它像这样

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

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


推荐阅读