[英]How to Access JSON Element
I have one json i wanted to print "formatted_address" element from that json 我有一个json,我想从该json打印“ formatted_address”元素
Json 杰森
{
"html_attributions" : [],
"results" : [
{
"formatted_address" : "Narayan Peth, Pune, Maharashtra 411030, India",
"geometry" : {
"location" : {
"lat" : 18.515797,
"lng" : 73.852335
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "3a25975b3806df28aa79ac4a8d954c307be4aa57",
"name" : "Aditya Medical",
"place_id" : "ChIJJxwmOHDAwjsRjRDO4LnGJ-I",
"reference" : "CmRSAAAA3E7ih55-2BZjRQcw_URQ2gwi8eWb5HU6hdfNUj_TqtDJ7TtASVMowcuWMkohNjp7F6UKuGsMuR-IlzZEt4YUJyzNxzWg-TYy6hyN8P5n2asAO6ztZeU3oHZdH7OBFFW_EhBe4cQbAU99oILcDmvv_gOhGhR7jzP0Z9-mDrncd5Gr9hOY7aOqRg",
"types" : [ "pharmacy", "health", "store", "point_of_interest", "establishment" ]
}
],
"status" : "OK"
}
I tired to print using but unable to print it. 我厌倦了使用来打印,但是无法打印。
foreach (json_decode($address[0]->Response) as $obj){
print_r($obj['results']['formatted_address']);
}
You need to set second param as true to get json as array. 您需要将第二个参数设置为true才能将json作为数组。 Also your
formatted_address
is agin inarray so need to pass index in it 另外您的
formatted_address
是agin inarray,因此需要在其中传递索引
foreach (json_decode($address[0]->Response, true) as $obj){
print_r($obj['results'][0]['formatted_address']);
}
That can be a solution : 那可以是一个解决方案:
$jsonAsArray = json_decode($yourJson, true);
$results = $array["results"][0];
var_dump($results['formatted_address']);
Good luck 祝好运
Try 尝试
foreach (json_decode($address[0]->Response, true) as $obj){
print_r($obj['results']['formatted_address']);
}
json_decode has a second parameter to determine the returned result format - object
or array
. json_decode具有第二个参数,用于确定返回的结果格式-
object
或array
。
I Try like @Plamen Nikolov, but but it doesn't work. 我尝试像@Plamen Nikolov一样,但不起作用。 And I Try change 'True' to 1 it's work!
然后我尝试将“ True”更改为1,就可以了!
foreach (json_decode($address[0]->Response, 1) as $obj){
print_r($obj['results']['formatted_address']);
}
Try 尝试
foreach (json_decode($address[0]->Response)->results as $obj){
print_r($obj->formatted_address);
}
json_decode($address[0]->Response) will give you an object. json_decode($ address [0]-> Response)将为您提供一个对象。 You should not use like array.
您不应该使用like数组。 so you should use "->" instead of array form.
因此,您应该使用“->”而不是数组形式。 Better you can put the result on foreach and get the data of formatted_address from that $obj
更好的是,您可以将结果放在foreach上,并从该$ obj获取formatted_address的数据
-- -
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.