用Json實(shí)現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法詳解
更新時(shí)間:2013年06月20日 11:22:46 作者:
本篇文章是對(duì)用Json實(shí)現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式。
簡(jiǎn)而論之,不管是xml還是json都是為了方便在客戶端與服務(wù)器端交互數(shù)據(jù)的中轉(zhuǎn)站,特別是用于對(duì)象型數(shù)據(jù),比如最常見(jiàn)的數(shù)組。
下面將分別將數(shù)組從php傳送給javascript,以及將數(shù)組從javascript傳送給php示例說(shuō)明,例子比較簡(jiǎn)單,明白概念即可。不管從php傳送給javascript,還是javascript傳送給php,json在傳送之前都會(huì)將對(duì)象扁平化即一維化為字符串。
PHP 向 JavaScript 傳值
PHP 文件 json.php
<?php
$arr = array(
'name' => '腳本之家',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.dbjr.com.cn',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
光執(zhí)行這個(gè)文件,其結(jié)果如下:
getProfile({"name":"u5e0cu4e9a","nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.dbjr.com.cn"}})
json.php 是通過(guò) json_encode 函數(shù)將數(shù)組扁平化,然后發(fā)送,相反有個(gè) json_decode 函數(shù)。
那么在 JavaScript 如何調(diào)用呢?很簡(jiǎn)單,定義一個(gè)變量獲取 PHP 傳來(lái)的 Json,該 Json 具備對(duì)象的特性,我們可以用 array.name 這種方式來(lái)獲取該 Json 的屬性。
<script type="text/javascript">
function getProfile(str) {
var arr = str;
document.getElementById('name').innerHTML = arr.name;
document.getElementById('nick').innerHTML = arr.nick;
document.getElementById('email').innerHTML = arr.contact.email;
document.getElementById('website').innerHTML = arr.contact.website;
}
</script>
<body>
<div id="name"></div>
<div id="nick"></div>
<div id="email"></div>
<div id="website"></div>
</body>
<script type="text/javascript" src="json.php"></script>
運(yùn)行結(jié)果如下:
腳本之家
Gonn
xxxxxxx@163.com
http://www.dbjr.com.cn
JavaScript 向 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:From javascript To php</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_password').value
}
var json_string = JSON.stringify(user);
document.getElementById('txt_json').value=json_string;
alert("點(diǎn)擊確定后將提交表單");
o.submit();
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="json_encode.php"onsubmit="JSON_test(this);return flase;">
<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扁平化需要一個(gè)插件:http://www.json.org/json2.js,通過(guò)JSON.stringify(str)將對(duì)象扁平化然后傳送給php。
注:另有一個(gè)http://www.json.org/json.js,對(duì)應(yīng)的是toJSONString方法。
var last=obj.toJSONString(); //針對(duì)json.js
var last=JSON.stringify(obj); //針對(duì)json2.js
json_encode.php
<?php
header('Content-Type: text/html; charset=utf-8');
$json_string = $_POST["txt_json"];
//echo $json_string;
if(ini_get("magic_quotes_gpc")=="1")
{
$json_string=stripslashes($json_string);
}
$user = json_decode($json_string);
echo var_dump($user);
echo '<br /><br /><br /><br />';
echo $user->name.'<br />';
echo $user->email.'<br />';
echo $user->password.'<br />';
?>
這里就需要用到j(luò)son_decode()這個(gè)函數(shù),然后調(diào)用其中數(shù)據(jù)用 $obj->屬性即可。
簡(jiǎn)而論之,不管是xml還是json都是為了方便在客戶端與服務(wù)器端交互數(shù)據(jù)的中轉(zhuǎn)站,特別是用于對(duì)象型數(shù)據(jù),比如最常見(jiàn)的數(shù)組。
下面將分別將數(shù)組從php傳送給javascript,以及將數(shù)組從javascript傳送給php示例說(shuō)明,例子比較簡(jiǎn)單,明白概念即可。不管從php傳送給javascript,還是javascript傳送給php,json在傳送之前都會(huì)將對(duì)象扁平化即一維化為字符串。
PHP 向 JavaScript 傳值
PHP 文件 json.php
復(fù)制代碼 代碼如下:
<?php
$arr = array(
'name' => '腳本之家',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.dbjr.com.cn',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
光執(zhí)行這個(gè)文件,其結(jié)果如下:
復(fù)制代碼 代碼如下:
getProfile({"name":"u5e0cu4e9a","nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.dbjr.com.cn"}})
json.php 是通過(guò) json_encode 函數(shù)將數(shù)組扁平化,然后發(fā)送,相反有個(gè) json_decode 函數(shù)。
那么在 JavaScript 如何調(diào)用呢?很簡(jiǎn)單,定義一個(gè)變量獲取 PHP 傳來(lái)的 Json,該 Json 具備對(duì)象的特性,我們可以用 array.name 這種方式來(lái)獲取該 Json 的屬性。
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function getProfile(str) {
var arr = str;
document.getElementById('name').innerHTML = arr.name;
document.getElementById('nick').innerHTML = arr.nick;
document.getElementById('email').innerHTML = arr.contact.email;
document.getElementById('website').innerHTML = arr.contact.website;
}
</script>
<body>
<div id="name"></div>
<div id="nick"></div>
<div id="email"></div>
<div id="website"></div>
</body>
<script type="text/javascript" src="json.php"></script>
運(yùn)行結(jié)果如下:
復(fù)制代碼 代碼如下:
腳本之家
Gonn
xxxxxxx@163.com
http://www.dbjr.com.cn
JavaScript 向 PHP 傳值
json_encode.html
復(fù)制代碼 代碼如下:
<!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:From javascript To php</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_password').value
}
var json_string = JSON.stringify(user);
document.getElementById('txt_json').value=json_string;
alert("點(diǎn)擊確定后將提交表單");
o.submit();
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="json_encode.php"onsubmit="JSON_test(this);return flase;">
<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扁平化需要一個(gè)插件:http://www.json.org/json2.js,通過(guò)JSON.stringify(str)將對(duì)象扁平化然后傳送給php。
注:另有一個(gè)http://www.json.org/json.js,對(duì)應(yīng)的是toJSONString方法。
復(fù)制代碼 代碼如下:
var last=obj.toJSONString(); //針對(duì)json.js
var last=JSON.stringify(obj); //針對(duì)json2.js
json_encode.php
復(fù)制代碼 代碼如下:
<?php
header('Content-Type: text/html; charset=utf-8');
$json_string = $_POST["txt_json"];
//echo $json_string;
if(ini_get("magic_quotes_gpc")=="1")
{
$json_string=stripslashes($json_string);
}
$user = json_decode($json_string);
echo var_dump($user);
echo '<br /><br /><br /><br />';
echo $user->name.'<br />';
echo $user->email.'<br />';
echo $user->password.'<br />';
?>
這里就需要用到j(luò)son_decode()這個(gè)函數(shù),然后調(diào)用其中數(shù)據(jù)用 $obj->屬性即可。
您可能感興趣的文章:
- PHP的Laravel框架結(jié)合MySQL與Redis數(shù)據(jù)庫(kù)的使用部署
- CentOS 安裝 PHP5.5+Redis+XDebug+Nginx+MySQL全紀(jì)錄
- PHP使用redis實(shí)現(xiàn)統(tǒng)計(jì)緩存mysql壓力的方法
- docker搭建php+nginx+swoole+mysql+redis環(huán)境的方法
- php中關(guān)于codeigniter的xmlrpc的類在進(jìn)行數(shù)據(jù)交換時(shí)的類型問(wèn)題
- PHP+Redis開(kāi)發(fā)的書(shū)簽案例實(shí)戰(zhàn)詳解
- php結(jié)合redis實(shí)現(xiàn)高并發(fā)下的搶購(gòu)、秒殺功能的實(shí)例
- php+redis實(shí)現(xiàn)商城秒殺功能
- php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)功能
- PHP結(jié)合Redis+MySQL實(shí)現(xiàn)冷熱數(shù)據(jù)交換應(yīng)用案例詳解
相關(guān)文章
用PHP ob_start()控制瀏覽器cache、生成html實(shí)現(xiàn)代碼
Output Control 函數(shù)可以讓你自由控制腳本中數(shù)據(jù)的輸出。它非常地有用,特別是對(duì)于:當(dāng)你想在數(shù)據(jù)已經(jīng)輸出后,再輸出文件頭的情況。2010-02-02PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能,涉及php針對(duì)mysql常見(jiàn)的連接、數(shù)據(jù)表查詢、遍歷、table表格構(gòu)成等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05dir()、readdir()、scandir()和glob()四種遍歷目錄方法及性能分析
php遍歷目錄和文件的場(chǎng)景在很多時(shí)候都能用到,遍歷目錄方法的方法有好幾種,那么應(yīng)該使用哪種方法呢?下面介紹dir()、readdir()、scandir()和glob()四種遍歷目錄方法及性能分析。2022-12-12php模仿asp Application對(duì)象在線人數(shù)統(tǒng)計(jì)實(shí)現(xiàn)方法
這篇文章主要介紹了php模仿asp Application對(duì)象在線人數(shù)統(tǒng)計(jì)實(shí)現(xiàn)方法,通過(guò)一個(gè)比較簡(jiǎn)單的自定義函數(shù)實(shí)現(xiàn)這一功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01php使用curl簡(jiǎn)單抓取遠(yuǎn)程url的方法
這篇文章主要介紹了php使用curl簡(jiǎn)單抓取遠(yuǎn)程url的方法,涉及php操作curl的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03PHP中調(diào)試函數(shù)debug_backtrace的使用示例代碼
debug_backtrace() 是一個(gè)很低調(diào)的函數(shù),很少有人注意過(guò)它,這篇文章主要給大家介紹了關(guān)于PHP中調(diào)試函數(shù)debug_backtrace的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的朋友們隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09php+ajax登錄跳轉(zhuǎn)登錄實(shí)現(xiàn)思路
這篇文章主要介紹了php+ajax登錄跳轉(zhuǎn)登錄實(shí)現(xiàn)思路,非常的簡(jiǎn)單,有需要的小伙伴可以參考下2016-07-07