PHP解析JSON_PHP如何使用JSON
2014-06-28 20:45:19 By: shinyuu
json_decode(对 JSON 格式的字符串进行编码)
格式:mixed json_decode ( string $json [, bool $assoc ] )
$json
待解码的 json string 格式的字符串。
$assoc
当该参数为 TRUE 时,将返回 array 而非 object 。
例1:
<?php $json = ´{"a":1,"b":2,"c":3,"d":4,"e":5}´; var_dump(json_decode($json)); var_dump(json_decode($json, true)); ?>
以上例程会输出:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
例2:
<?php $json_string=´{"id":1,"name":"mike","country":"usa","office":["microsoft","oracle"]} ´; $obj=json_decode($json_string); ?>
可以使用以下的方法来输出和访问
echo $obj->name; //displays mike
echo $obj->office[0]; //displays microsoft
注意:
(1)使用UTF-8编码
(2)不能在最后元素有逗号
(3)不能使用单引号
(4)不能有 , ,如果有请替换
若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力
想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)
或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)
如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教
为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)
感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛
啊6站长网 2016-08-15 21:54:20 1 评 | 回复
SITEMAP X效果怎么样了?www.are6.com