繁体   English   中英

使用Laravel从数组中获取JSON值

[英]Get a JSON value from an array using Laravel

我试图从他们的地理编码服务中获取JSON数组中的纬度和经度值 - 从Google返回的$response

JSON数组是这样返回的(随机地址):

{
   "results":[
      {
         "address_components":[
            {
               "long_name":"57",
               "short_name":"57",
               "types":[
                  "street_number"
               ]
            },
            {
               "long_name":"Polo Gardens",
               "short_name":"Polo Gardens",
               "types":[
                  "route"
               ]
            },
            {
               "long_name":"Bucksburn",
               "short_name":"Bucksburn",
               "types":[
                  "sublocality_level_1",
                  "sublocality",
                  "political"
               ]
            },
            {
               "long_name":"Aberdeen",
               "short_name":"Aberdeen",
               "types":[
                  "locality",
                  "political"
               ]
            },
            {
               "long_name":"Aberdeen",
               "short_name":"Aberdeen",
               "types":[
                  "postal_town"
               ]
            },
            {
               "long_name":"Aberdeen City",
               "short_name":"Aberdeen City",
               "types":[
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name":"United Kingdom",
               "short_name":"GB",
               "types":[
                  "country",
                  "political"
               ]
            },
            {
               "long_name":"AB21 9JU",
               "short_name":"AB21 9JU",
               "types":[
                  "postal_code"
               ]
            }
         ],
         "formatted_address":"57 Polo Gardens, Aberdeen, Aberdeen City AB21 9JU, UK",
         "geometry":{
            "location":{
               "lat":57.1912463,
               "lng":-2.1790257
            },
            "location_type":"ROOFTOP",
            "viewport":{
               "northeast":{
                  "lat":57.19259528029149,
                  "lng":-2.177676719708498
               },
               "southwest":{
                  "lat":57.18989731970849,
                  "lng":-2.180374680291502
               }
            }
         },
         "partial_match":true,
         "place_id":"ChIJLTex1jQShEgR5UJ2DNc6N9s",
         "types":[
            "street_address"
         ]
      }
   ],
   "status":"OK"
}

我尝试过以下方法:

json_decode($response->results->geometry->location->lat)

但它返回'试图访问非对象的属性'。

任何帮助将非常感激。

var_dump(json_decode($response)->results[0]->geometry->location->lat);

将true作为第二个参数传递给json_decode函数并使用数组表示法:

$obj = json_decode($json_string2, true);
foreach ($obj['geometry'] as $key => $value) {
    $lat = $value['location']['lat'];
    $long   = $value['location']['lng'];
}

试试这个: $jsonArray = json_decode($response,true);

它只是,首先你要用json_decode函数将整个json字符串转换为数组或对象,然后使用结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM