PHPer都知道PHP是不支持字面量了,至少目前版本都不支持。比如,在JS中可以这样定义object
var o = { 'name' : 'qttc' , 'url' : 'www.nhooo.com' }; alert(o.name);
o = { 'name' : 'qttc' , 'url' : 'www.nhooo.com' } print o['name']
$a = { "name" : "qttc", "url" : "www.nhooo.com" };
[root@lee www]# php a.php PHP Parse error: syntax error, unexpected '{' in /data0/htdocs/www/a.php on line 4
$a = '{ "name" : "qttc", "url" : "www.nhooo.com" }'; $a = json_decode($a); print_r($a);
[root@lee www]# php a.php stdClass Object ( [name] => qttc [url] => www.nhooo.com )
$a = '{ "name" : "qttc", "url" : "www.nhooo.com" , "arr":["zhangsan","lisi"] }'; $a = json_decode($a); print_r($a);
[root@lee www]# php a.php stdClass Object ( [name] => qttc [url] => www.nhooo.com [arr] => Array ( [0] => zhangsan [1] => lisi ) )