对比
JSON优势:变量序列化后依然可读,可以给其他系统使用,因为JSON格式是标准的
JSON劣势:
只对UFT-8的数据有效,其他编码可能不能很好工作
只对stdClass类的示例有效,对于私有属性会忽略
Serialize优势:允许非UTF-8的变量 ,支持除了stdClass 示例外的其他实例
Serialize劣势:编码后的文本对人来说是不可读的,无法被其他语言的系统引用
代码
$web = new stdClass;
$web->site = 'tantengvip';
$web->owner = 'tuntun';
$web->age = 5;
//和
$web = array();
$web['site'] = 'tantengvip';
$web['owner'] = 'tuntun';
$web['age'] = 5;
var_dump(serialize($web));
var_dump(unserialize(serialize($web)));
var_dump(json_encode($web));
var_dump(json_decode(json_encode($web)));
string 'O:8:"stdClass":3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}' (length=87)
object(stdClass)[127]
public 'site' => string 'tantengvip' (length=10)
public 'owner' => string 'tuntun' (length=6)
public 'age' => int 5
string '{"site":"tantengvip","owner":"tuntun","age":5}' (length=46)
object(stdClass)[127]
public 'site' => string 'tantengvip' (length=10)
public 'owner' => string 'tuntun' (length=6)
public 'age' => int 5
var_dump(serialize($web));
var_dump(unserialize(serialize($web)));
var_dump(json_encode($web));
var_dump(json_decode(json_encode($web),true))
string 'a:3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}' (length=74)
array (size=3)
'site' => string 'tantengvip' (length=10)
'owner' => string 'tuntun' (length=6)
'age' => int 5
string '{"site":"tantengvip","owner":"tuntun","age":5}' (length=46)
array (size=3)
'site' => string 'tantengvip' (length=10)
'owner' => string 'tuntun' (length=6)
'age' => int 5