在PHP上进行JSON编码和解码
我对JSON输出进行了编码在PHP上进行JSON编码和解码,php,json,Php,Json,我对JSON输出进行了编码 $responseJSON {"status":1,"content":{"sessionid":"4c86cf1acac07811db6ec670e0b9cdd2"}} 现在我对它进行解码 $decoded=json_decode($responseJSON); print_r($decoded) 我明白了 stdClass Object ( [status] => 1 [content] => stdClass Object (
$responseJSON
{"status":1,"content":{"sessionid":"4c86cf1acac07811db6ec670e0b9cdd2"}}
现在我对它进行解码
$decoded=json_decode($responseJSON);
print_r($decoded)
我明白了
stdClass Object (
[status] => 1
[content] => stdClass Object (
[sessionid] => 4c86cf1acac07811db6ec670e0b9cdd2
)
)
我不想那样解码
如何在没有stdClass标签的情况下解码到普通数组?试试看
json_decode($responseJSON,true);
true
告诉php生成关联数组第二个参数可以是true
,它将强制所有对象作为php关联数组读入
$decoded = json_decode( $responseJSON, TRUE );
如果为TRUE(参考第二个参数),则返回的对象将转换为关联数组
使用:
没有足够的代表评论其他人的评论 在处理完信息后,将其公开
$decoded = json_decode( $responseJSON, TRUE );
您可以正常访问其中的所有信息。
做一个
以防万一,它会增加你意想不到的级别
那就照常进行吧
echo $decoded['status']
echo $decoded['content']['sessionid']
作为旁注,
stdClass
是一个特殊的泛型类,用于非从普通类构造的对象。解码后如何提取$decoded->status中的数据?我没有得到任何东西。解码后如何提取$decoded->status中的数据$解码->内容->会话ID?我没有得到任何类似的结果。@lilzz,因为它是一个数组,所以您应该使用数组表示法,比如$decoded['status']。
var_dump($decoded);
echo $decoded['status']
echo $decoded['content']['sessionid']