PHP 上的 JSON 编码和解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7707618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON encode and decode on PHP
提问by lilzz
I have JSON output encoded.
我有 JSON 输出编码。
$responseJSON
{"status":1,"content":{"sessionid":"4c86cf1acac07811db6ec670e0b9cdd2"}}
now I do a decode on that
现在我做了一个解码
$decoded=json_decode($responseJSON);
print_r($decoded)
I get
我得到
stdClass Object (
[status] => 1
[content] => stdClass Object (
[sessionid] => 4c86cf1acac07811db6ec670e0b9cdd2
)
)
I don't want decoded like that.
我不想那样解码。
how do I decode to an normal array without those stdClass tag?
如何解码为没有那些 stdClass 标签的普通数组?
回答by Bankzilla
Don't have enough rep to comment on other peoples comments
没有足够的代表来评论其他人的评论
To get the info out after you've processed it with
在处理后获取信息
$decoded = json_decode( $responseJSON, TRUE );
You can access all the information inside of it as normal. do a
您可以正常访问其中的所有信息。做一个
var_dump($decoded);
just incase it add's levels you wouldn't expect Then just proceed as usual
以防万一它增加了你意想不到的水平然后照常进行
echo $decoded['status']
echo $decoded['content']['sessionid']
回答by Foo Bah
try
尝试
json_decode($responseJSON,true);
the true
tells php to generate associative arrays
将true
告诉PHP生成关联数组
回答by Kendall Hopkins
json_decode
second argument can be TRUE
which will force all objects to be read in as a PHP associated arrays.
json_decode
第二个参数可以是TRUE
强制所有对象作为 PHP 关联数组读入。
$decoded = json_decode( $responseJSON, TRUE );
When TRUE (referring to the second argument), returned objects will be converted into associative arrays.
当为 TRUE(指第二个参数)时,返回的对象将被转换为关联数组。
回答by imm
Use:
用:
$decoded=json_decode($responseJSON, TRUE);