PHP串行化与JSON
2008-06-19 02:36:52 来源:baidu.com
| WebjxCom提示:串行化即将变量转换成字节流的过程。 |
json_decode函数实例
下面两个例子都基于我们的一个情景假设,即,我们有一个用户注册的模块,这个模块以“面向对象”的方式工作,在json_decode函数实例中,我们在前台将用户的注册信息变为一个类的属性,而后传递到后台的php文件(这里为了简便,就不用Ajax了)。在json_encode实例中,我们在html文件中引用一个js文件,地址指向php文件,在php文件中输出json编码后的用户对象(同样为了简便,我们直接生成一个对象而不从数据库中取信息),并在html中输出。
好了,先来看前台的页面json_encode.htm,这个页面模仿了通常的注册页面,在其上面有一个表单,当提交时,触发JavaScript函数,生成一个用户对象user,将表单内容设为用户对象的属性,生成JSON文本,以POST方式传递到后台的json_encode.php文件。在js_encode.php文件中,将JSON文本用json_decode函数解析为PHP对象,并输出。
好了,先来看json_encode.html文件,文件代码如下:
代码:
<!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>下面到json_encode.php文件,如下:
代码:
<?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) "example@domain.com"
["password"]=>
string(10) "hanguofeng"
}在这个例子中,仍然是由两部分构成的,即json_enode.html和json_encode.php。在json_decode.html文件中,基本与json_decode.html文件的表单类似,但是不同的是,这次我们要从json_encode.php文件中获得相应用户的JSON文本,先来看这个PHP文件吧:
代码:
<?php
Class user
{
public $name="";
public $email="";
public $password="";
};
$myUser = new user;
$myUser->name="hanguofeng";
$myUser->email="example@domain.com";
$myUser->password="hanguofeng";
$json_string = json_encode($myUser);
?>
var user = <?php echo($json_string)?>;接下,我们需要在json_encode.html文件中引入json_encode.php文件,并得到user对象的各个属性,如下:
代码:
<!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>我们在文本框代码后面加入JavaScript的代码,对文本框的value属性进行操作,分别设定其值为user对象的相应值。
上一篇:19岁黑客推销网络安全产品获罪
