我已经将JSON文件解码为变量($tmp
)。var_dump($tmp)
给:
object(stdClass)#31 (3) {
["c"]=> int(2)
["r"]=> int(2)
["d"]=> object(stdClass)#32 (4) {
["1"]=> string(2) "un"
["2"]=> string(4) "deux"
["3"]=> string(5) "trois"
["4"]=> string(6) "quatre"
}
}
我想检索例如"un",所以我做$tmp->d["1"]
,但它不起作用。我得到了以下错误:
Fatal error: Cannot use object of type stdClass as array in File.php on line 17
json_decode接受一个附加参数,该参数将把json字符串转换为数组而不是对象
json_decode($json_str, true)
正如注释所指出的,你的json对象的d
属性是一个对象而不是一个数组,所以你不能用数组符号访问它(正如你看到的有一个错误)
I believe
$tmp->d->{'1'}
// "un"
应该可以访问
php有一个默认函数json_encode和json_decode
$arrayOfValues = array();
$jsonString = json_encode($arrayOfValues);
和
$arrayOfValues = json_decode($jsonString);