简体   繁体   English

在php中读取JSON格式的文件

[英]Reading a JSON formatted file in php

I'm very new at JSON, I'm trying to read one parameter of this file using php: 我是JSON的新手,正在尝试使用php读取此文件的一个参数:

stdClass Object
(
    [status] => OK
    [results] => Array
        (
            [0] => stdClass Object
                (
                    [types] => Array
                        (
                            [0] => route
                        )

                    [formatted_address] => Foro Umberto I, 90133 Palermo, Italy
                    [address_components] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [long_name] => Foro Umberto I
                                    [short_name] => SS113
                                    [types] => Array
                                        (
                                            [0] => route
                                        )

                                )

This is only a part, anyway, i need to echo the formatted_address using php, but i can get it, this is part of my code: 无论如何,这只是一部分,我需要使用php来回显formatted_address ,但是我可以得到,这是我的代码的一部分:

$address = json_decode($curlData);
echo($address -> {'formatted_address'});

Thanks for any help! 谢谢你的帮助!

$data = json_decode($curlData);
$result = $data->results[0];
$address = $result->formatted_address;

I don't think you need the brackets or the quotes: 我认为您不需要括号或引号:

echo $address -> formatted_address;

Also, for what it's worth, when you use json_decode you can also have it return an array instead of an object by telling it true in the second argument: 此外,就其价值而言,当您使用json_decode时,还可以通过在第二个参数中将其告诉true来使其返回数组而不是对象:

$address = json_decode($whatever, true);
echo $address['formatted_address'];

...If that helps you any. ...如果有帮助的话。

解码json后,您只有一个普通的旧PHP对象,因此您可以执行以下操作:

$address->formatted_address

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

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