PHP json_decode returns 数组而不是对象
PHP json_decode returns array and NOT an object
我必须用 PHP 做一个网站,实际上我正在尝试。现在我想从 URL 获得一个 JSON(我有一个带有 Node.js 的 Web 服务)并显示在屏幕上。 URL return 是一个 JSON 对象,如下所示:
[{"name":"Juan","text":"Oh my god"},{"name":"Pedro","text":"I'm here"}]
我在 PHP 文件中有此代码:
<?php
$data = file_get_contents('http://localhost:3000/node/busca'); // Returns the JSON
$terminos = json_decode($data);
print_r($terminos);
echo $terminos->name;
?>
但是print_r
returns:
Array (
[0] => stdClass Object (
[name] => Juan
[text] => Oh my god
)
[1] => stdClass Object (
[name] => Pedro
[text] => I'm here
)
)
回声说
Notice: Trying to get property of non-object in C:...\index.php on line 17
我能做什么? json_decode
应该 return 一个对象而不是一个数组。
JSON和解码后的PHP是一个对象数组。尝试:
echo $terminos[0]->name;
您有多个数组元素,因此:
foreach($terminos as $object) {
echo $object->name;
}
您的数据是经过编码的对象数组。所以你会得到一个对象数组。一切都在这里。
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
$terminosFalse = json_decode($data, true);
array(2) {
[0]=>
array(1) {
["attribute"]=>
string(1) "attr1"
}
[1]=>
array(1) {
["attribute"]=>
string(1) "ATTR2"
}
}
$terminosTrue = json_decode($data, false);
array(2) {
[0]=>
object(stdClass)#1 (1) {
["attribute"]=>
string(1) "attr1"
}
[1]=>
object(stdClass)#2 (1) {
["attribute"]=>
string(1) "ATTR2"
}
}
编辑 OP 的问题以将数组输出重新格式化为:
Array (
[0] => stdClass Object (
[name] => Juan
[text] => Oh my god
)
[1] => stdClass Object (
[name] => Pedro
[text] => I'm here
)
)
像这样看,很清楚各个对象是如何包装和寻址的:
foreach ($terminos as $idx => $obj ) {
echo "Name $idx: " $obj->name . PHP_EOL;
/// ... etc
}
应该输出:
Name 0: Juan
Name 1: Pedro
我必须用 PHP 做一个网站,实际上我正在尝试。现在我想从 URL 获得一个 JSON(我有一个带有 Node.js 的 Web 服务)并显示在屏幕上。 URL return 是一个 JSON 对象,如下所示:
[{"name":"Juan","text":"Oh my god"},{"name":"Pedro","text":"I'm here"}]
我在 PHP 文件中有此代码:
<?php
$data = file_get_contents('http://localhost:3000/node/busca'); // Returns the JSON
$terminos = json_decode($data);
print_r($terminos);
echo $terminos->name;
?>
但是print_r
returns:
Array (
[0] => stdClass Object (
[name] => Juan
[text] => Oh my god
)
[1] => stdClass Object (
[name] => Pedro
[text] => I'm here
)
)
回声说
Notice: Trying to get property of non-object in C:...\index.php on line 17
我能做什么? json_decode
应该 return 一个对象而不是一个数组。
JSON和解码后的PHP是一个对象数组。尝试:
echo $terminos[0]->name;
您有多个数组元素,因此:
foreach($terminos as $object) {
echo $object->name;
}
您的数据是经过编码的对象数组。所以你会得到一个对象数组。一切都在这里。
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
$terminosFalse = json_decode($data, true);
array(2) {
[0]=>
array(1) {
["attribute"]=>
string(1) "attr1"
}
[1]=>
array(1) {
["attribute"]=>
string(1) "ATTR2"
}
}
$terminosTrue = json_decode($data, false);
array(2) {
[0]=>
object(stdClass)#1 (1) {
["attribute"]=>
string(1) "attr1"
}
[1]=>
object(stdClass)#2 (1) {
["attribute"]=>
string(1) "ATTR2"
}
}
编辑 OP 的问题以将数组输出重新格式化为:
Array (
[0] => stdClass Object (
[name] => Juan
[text] => Oh my god
)
[1] => stdClass Object (
[name] => Pedro
[text] => I'm here
)
)
像这样看,很清楚各个对象是如何包装和寻址的:
foreach ($terminos as $idx => $obj ) {
echo "Name $idx: " $obj->name . PHP_EOL;
/// ... etc
}
应该输出:
Name 0: Juan
Name 1: Pedro