分享

用 Json 来实现 PHP 与 JavaScript 间数据交换

 每天学一点7 2013-12-04

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。

简而论之,不管是xml还是json都是为了方便在客户端与服务器端交互数据的中转站,特别是用于对象型数据,比如最常见的数组。

下面将分别将数组从php传送给javascript,以及将数组从javascript传送给php示例说明,例子比较简单,明白概念即可。不管从php传送给javascript,还是javascript传送给php,json在传送之前都会将对象扁平化即一维化为字符串。

PHP 向 JavaScript 传值

PHP 文件 json.php

01<?php
02    $arr array(
03        'name' => '希亚',
04        'nick' => 'Gonn',
05        'contact' => array(
06            'email' => 'gonnsai@163.com',
07            'website' => 'http://www.',
08        )
09    );
10    $json_string = json_encode($arr);
11    echo "getProfile($json_string)";
12?>

光执行这个文件,其结果如下:

1getProfile({"name":"\u5e0c\u4e9a","nick":"Gonn",
2    "contact":{"email":"gonnsai@163.com","website":"http:\/\/www."}})

json.php 是通过 json_encode 函数将数组扁平化,然后发送,相反有个 json_decode 函数。

那么在 JavaScript 如何调用呢?很简单,定义一个变量获取 PHP 传来的 Json,该 Json 具备对象的特性,我们可以用 array.name 这种方式来获取该 Json 的属性。

01<script type="text/javascript">
02function getProfile(str) { 
03    var arr = str; 
04    document.getElementById('name').innerHTML = arr.name; 
05    document.getElementById('nick').innerHTML = arr.nick; 
06    document.getElementById('email').innerHTML = arr.contact.email;
07    document.getElementById('website').innerHTML = arr.contact.website;
08
09</script>
10<body>
11<div id="name"></div>
12<div id="nick"></div>
13<div id="email"></div>
14<div id="website"></div>
15</body>
16<script type="text/javascript" src="json.php"></script>

运行结果如下:

1希亚
2Gonn
3gonnsai@163.com
4http://www.

JavaScript 向 PHP 传值

json_encode.html

01<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
02<html xmlns="http://www./1999/xhtml">
03<head>
04<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
05<title>json:From javascript To php</title>
06<script src="json2.js" type="text/javascript"></script>
07<script type="text/javascript">
08function JSON_test(o)
09{
10    var user = {
11        name:document.getElementById('txt_name').value,
12        email:document.getElementById('txt_email').value,
13        password:document.getElementById('txt_password').value
14    }
15    var json_string = JSON.stringify(user);
16    document.getElementById('txt_json').value=json_string;
17    alert("点击确定后将提交表单");
18    o.submit();
19}
20</script>
21</head>
22  
23<body>
24  
25    <form id="form1" name="form1" method="post" action="json_encode.php"onsubmit="JSON_test(this);return flase;">
26        <label for="txt_name">姓名</label>
27        <p><input type="text" name="txt_name" id="txt_name" /></p>
28        <label for="txt_email">邮箱</label>
29        <p><input type="text" name="txt_email" id="txt_email" /></p>
30        <p><label for="txt_password">密码</label></p>
31        <p><input type="text" name="txt_password" id="txt_password" /></p>
32        <p><input type="text" name="txt_json" id="txt_json" />
33            <label for="button"></label>
34            <input type="submit" name="button" id="button" value="JSON" />
35        </p>
36    </form>
37  
38</body>
39</html>

这里javascript扁平化需要一个插件:http://www./json2.js,通过JSON.stringify(str)将对象扁平化然后传送给php。

注:另有一个http://www./json.js,对应的是toJSONString方法。

1var last=obj.toJSONString(); //针对json.js
2var last=JSON.stringify(obj); //针对json2.js

json_encode.php

01<?php
02    header('Content-Type: text/html; charset=utf-8');
03    $json_string $_POST["txt_json"];
04    //echo $json_string;
05    if(ini_get("magic_quotes_gpc")=="1")
06    {
07        $json_string=stripslashes($json_string);
08    }
09    $user = json_decode($json_string);
10  
11    echo var_dump($user);
12  
13    echo '<br /><br /><br /><br />';
14    echo $user->name.'<br />';
15    echo $user->email.'<br />';
16    echo $user->password.'<br />';
17?>

这里就需要用到json_decode()这个函数,然后调用其中数据用 $obj->属性即可。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多