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>
当提交表单时,将触发JavaScript函数JSON_text(),该函数首先建立一个JavaScript对象user,将其name、email和password属性分别设为对应表单的值,而后使用json2.js文件的JSON.stringify方法将其转换为JSON文本json_string,最后设定隐藏域(这里为了使你看的清楚,我把这个隐藏域以文本框形式显示了)txt_json的值为json_string,并提交表单。
下面到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);
?>
在这个文件中,首先得到json_encode.html文件中POST表单域txt_json的值,放入变量$json_string中,而后判断,如果当前PHP的设定为magic_quotes_gpc=On,即传入的双引号等会被转义,这样json_decode函数无法解析,因此我们要将其反转义化。而后,使用json_decode函数将JSON文本转换为对象,保存在$user变量中,最终用echo var_dump($user);,将该对象dump输出出来,最终结果是:
代码:
object(stdClass)#1 (3) {
   ["name"]=>
   string(10) "hanguofeng"
   ["email"]=>
   string(18) "example@domain.com"
   ["password"]=>
   string(10) "hanguofeng"
}
json_encode函数实例

在这个例子中,仍然是由两部分构成的,即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)?>;
这个文件首先建立类user,而后获得一个user类的实例myUser,并设定其用户名、邮箱和密码,接下来使用json_encode函数将其转换为JSON文本,保存在变量$json_string中,最后输出一段JavaScript代码,以在JavaScript中建立全局变量user。
接下,我们需要在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>
在这个文件中,你需要注意两点,第一是,我们以这样的代码引入json_encode.php文件为JavaScript文件:
代码:
<script src="json_encode.php" type="text/javascript"></script>
第二点则是:
我们在文本框代码后面加入JavaScript的代码,对文本框的value属性进行操作,分别设定其值为user对象的相应值。
共7页: 上一页 [1] [2] [3] [4] [5] [6] 7 下一页