无法在 PHP 中的 json_encode 之后检索 JSON 对象属性的值
Can't retrieve value of JSON Object attr after json_encode in PHP
我无法获得此(显然有效)JSON 对象中 image
属性的值:
echo var_dump($result);
array(1) {
["images"]=>
array(1) {
[0]=>
array(1) {
["src"]=>
string(112) "http://staticf5a.diaadia.info/sites/default/files/styles/landscape_310_155/public/nota_periodistica/taxis_13.jpg"
}
}
}
$jsonResult = json_encode($result); //result is an array of arrays
echo $jsonResult;
{"images":[{"src":"http:\/\/staticf5a.diaadia.info\/sites\/default\/files\/styles\/landscape_310_155\/public\/nota_periodistica\/taxis_13.jpg"}]}
echo $jsonResult->images; //show nothing
此代码段在几天前工作,日志 (ini_set('display_errors', '0');
) 未显示任何相关内容。
编码后,$jsonResult
只是一个字符串,如果不先对其进行解码,您将无法访问已编码 JSON 的任何元素。
看看 PHP 的 ``json_decode'' 函数:http://php.net/manual/pl/function.json-decode.php
它会将 JSON 转换回关联数组或对象。
无论如何,我不知道为什么你将关联数组编码为 JSON 并尝试在那里访问 images
而不是仅仅从数组本身获取它。
您正在尝试从此处的字符串中获取 属性。 json_encode()
returns json 对象作为字符串的表示。您可以使用 json_decode()
将该字符串转换为实际对象
我无法获得此(显然有效)JSON 对象中 image
属性的值:
echo var_dump($result);
array(1) {
["images"]=>
array(1) {
[0]=>
array(1) {
["src"]=>
string(112) "http://staticf5a.diaadia.info/sites/default/files/styles/landscape_310_155/public/nota_periodistica/taxis_13.jpg"
}
}
}
$jsonResult = json_encode($result); //result is an array of arrays
echo $jsonResult;
{"images":[{"src":"http:\/\/staticf5a.diaadia.info\/sites\/default\/files\/styles\/landscape_310_155\/public\/nota_periodistica\/taxis_13.jpg"}]}
echo $jsonResult->images; //show nothing
此代码段在几天前工作,日志 (ini_set('display_errors', '0');
) 未显示任何相关内容。
编码后,$jsonResult
只是一个字符串,如果不先对其进行解码,您将无法访问已编码 JSON 的任何元素。
看看 PHP 的 ``json_decode'' 函数:http://php.net/manual/pl/function.json-decode.php
它会将 JSON 转换回关联数组或对象。
无论如何,我不知道为什么你将关联数组编码为 JSON 并尝试在那里访问 images
而不是仅仅从数组本身获取它。
您正在尝试从此处的字符串中获取 属性。 json_encode()
returns json 对象作为字符串的表示。您可以使用 json_decode()