当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP串行化与JSON
原文连接:http://hi.baidu.com/lostdays/blog/item/8d76c300ec4e3c15738b65fa.html
总目录
What 、Why、How
What
Why
HowPHP串行化语法
PHP串行化实例
在JavaScript中串行化为JSON—使用json2.js
在JavaScript中串行化为JSON—使用prototype.js
json_decode函数
json_encode函数
json_decode函数实例
json_encode函数实例
背景说明
前台JavaScript部分
后台PHP部分
{"type":"human","name":"hanguofeng","age":22}则是一个JSON表达式,他保存了一个对象,我们如何将它恢复为对象呢?很简单,如下:
代码:
var animal_str = '{"type":"human","name":"hanguofeng","age":22}';
var animal2=eval('(' + animal_str + ')');
a:3:{s:4:"type";s:5:"human";s:4:"name";s:10:"hanguofeng";s:3:"age";s:2:"20";}
$animal =
array
(
"type" => "human",
"name" => "hanguofeng",
"age" => "20"
);OK,上面的一些介绍只是让你大致看到串行化和JSON是什么样的东西,你无须对这里的代码过分纠结,我们在后面会详细讲解的,下面我们来谈谈为什么要使用串行化。
<?php
$animal =
array
(
"type" => "human",
"name" => "hanguofeng",
"age" => "20"
);
?>Array
(
[type] => human
[name] => hanguofeng
[age] => 20
)<?php
$animal =
array
(
"type" => "human",
"name" => "hanguofeng",
"age" => "20"
);
$animal_ser=serialize($animal);
echo($animal_ser);
?>a:3:{s:4:"type";s:5:"human";s:4:"name";s:10:"hanguofeng";s:3:"age";s:2:"20";}<?php
$animal_ser='a:3:{s:4:"type";s:5:"human";s:4:"name";s:10:"hanguofeng";s:3:"age";s:2:"20";}';
$animal = unserialize($animal_ser);
print_r($animal);
?>Array
(
[type] => human
[name] => hanguofeng
[age] => 20
)这样我们就完成了数组的反串行化。
<?php
class A {
var $one = 1;
function show_one() {
echo $this->one;
}
}
?><?php
include("classa.inc");
$a=new A;
echo(serialize($a));
?>O:1:"A":1:{s:3:"one";i:1;}<?php
include("classa.inc");
$s = 'O:1:"A":1:{s:3:"one";i:1;}';
$a = unserialize($s);
$a->show_one();
?>此时输出“1”,即调用了A类的show_one()方法。
你可以注意到虽然在实例的串行化字符串中并没有包含类的方法,但是我们将其反串行化后,仍然可以调用类的方法,这个特性在PHP4及以上版本中被支持(当然,你需要包含类的定义文件classa.inc)。
注:你可以参考PHP手册中Language Reference->Classes and Objects->Serializing objects - objects in sessions一节的内容。
JSON.stringify(value, whitelist)var text = JSON.stringify(['e', {pluribus: 'unum'}]);
//text is '["e",{"pluribus":"unum"}]'
JSON.parse(text, filter)//解析文本,如果某个键包含字符串“date”,则将其值转换为日期
myData = JSON.parse(text, function (key, value) {
return key.indexOf('date') >= 0 ? new Date(value) : value;
});var myE = eval('["e",{"pluribus":"unum"}]');来获得对象myE。
var cat=
{
name:"hellokitty",
height:"6 apples"
}
alert(Object.toJSON(cat));
//将弹出对话框,内容为 {"name": "hellokitty", "height": "6 apples"}另外,在prototype.js中还有另外的JSON支持,主要是在Ajax对象中对Ajax返回请求中JSON内容的解析。这里暂时与我们的内容无关,也不再介绍了。
PHP与JSON
在上面我们一起了解了PHP进行对象串行化的方法以及在JavaScript中进行将对象串行化为JSON的方法,你大致会质疑我为什么将二者放在一起,因为他们的语法实际是不完全一样的,然而,在PHP中,可以对JSON文本进行反串行化,也可以将PHP的对象串行化为JSON而非PHP风格的文本。这主要是靠json_decode和json_encode两个函数来完成的,需要特别说明的是,这两个函数在PHP 5 >= 5.2.0中才被支持,如果你要编写运行在PHP4环境下的程序,那么这两个函数是不可以使用的。
json_decode函数
语法
mixed json_decode ( string $json [, bool $assoc] )
获取一个JSON编码文本,并且将其转换为PHP变量
参数
json
被JSON编码的文本
assoc
当为TRUE时,返回的值为联合数组
返回值
返回一个对象,或者如果可选的assoc参数为TRUE,则一个联合数组将会被返回
json_encode函数
语法
string json_encode ( mixed $value )
该函数返回一个值的JSON表达式
参数
value
要被编码的值,可以为除resource外的任何类型参数
这个函数仅在UTF-8编码格式时起作用
返回值
当成功时返回编码后的JSON文本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>json_decode</title>
<script src="json2.js" type="text/javascript"></script>
<script type="text/javascript">
function JSON_test(o)
{
var user = {
name:document.getElementById('txt_name').value,
email:document.getElementById('txt_email').value,
password:document.getElementById('txt_name').value
}
var json_string = JSON.stringify(user);
document.getElementById('txt_json').value=json_string;
alert("点击确定后将提交表单");
o.submit();
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="json_encode.php" onsubmit="JSON_test(this)">
<label for="txt_name">姓名</label>
<p>
<input type="text" name="txt_name" id="txt_name" />
</p>
<label for="txt_email">邮箱</label>
<p>
<input type="text" name="txt_email" id="txt_email" />
</p>
<p>
<label for="txt_password">密码</label>
</p>
<p>
<input type="text" name="txt_password" id="txt_password" />
</p>
<p>
<input type="text" name="txt_json" id="txt_json" />
<label for="button"></label>
<input type="submit" name="button" id="button" value="JSON" />
</p>
</form>
</body>
</html><?php
$json_string = $_POST["txt_json"];
if(ini_get("magic_quotes_gpc")=="1")
{
$json_string=stripslashes($json_string);
}
$user = json_decode($json_string);
echo var_dump($user);
?>object(stdClass)#1 (3) {
["name"]=>
string(10) "hanguofeng"
["email"]=>
string(18) "[email protected]"
["password"]=>
string(10) "hanguofeng"
}<?php
Class user
{
public $name="";
public $email="";
public $password="";
};
$myUser = new user;
$myUser->name="hanguofeng";
$myUser->email="[email protected]";
$myUser->password="hanguofeng";
$json_string = json_encode($myUser);
?>
var user = <?php echo($json_string)?>;<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>json_encode</title>
<script src="json_encode.php" type="text/javascript"></script>
</head>
<body>
<form id="form1" name="form1" method="post">
<label for="txt_name">姓名</label>
<p>
<input type="text" name="txt_name" id="txt_name" />
</p>
<label for="txt_email">邮箱</label>
<p>
<input type="text" name="txt_email" id="txt_email" />
</p>
<p>
<label for="txt_password">密码</label>
</p>
<p>
<input type="text" name="txt_password" id="txt_password" />
</p>
</form>
<script type="text/javascript" >
document.getElementById('txt_name').value=user.name;
document.getElementById('txt_email').value=user.email;
document.getElementById('txt_password').value=user.password;
</script>
</body>
</html><script src="json_encode.php" type="text/javascript"></script>
评论 (0) All