用Json實現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法詳解
更新時間:2013年06月20日 11:22:46 作者:
本篇文章是對用Json實現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法進行了詳細的分析介紹,需要的朋友參考下
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式。
簡而論之,不管是xml還是json都是為了方便在客戶端與服務(wù)器端交互數(shù)據(jù)的中轉(zhuǎn)站,特別是用于對象型數(shù)據(jù),比如最常見的數(shù)組。
下面將分別將數(shù)組從php傳送給javascript,以及將數(shù)組從javascript傳送給php示例說明,例子比較簡單,明白概念即可。不管從php傳送給javascript,還是javascript傳送給php,json在傳送之前都會將對象扁平化即一維化為字符串。
PHP 向 JavaScript 傳值
PHP 文件 json.php
<?php
$arr = array(
'name' => '腳本之家',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.dhdzp.com',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
光執(zhí)行這個文件,其結(jié)果如下:
getProfile({"name":"u5e0cu4e9a","nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.dhdzp.com"}})
json.php 是通過 json_encode 函數(shù)將數(shù)組扁平化,然后發(fā)送,相反有個 json_decode 函數(shù)。
那么在 JavaScript 如何調(diào)用呢?很簡單,定義一個變量獲取 PHP 傳來的 Json,該 Json 具備對象的特性,我們可以用 array.name 這種方式來獲取該 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>
運行結(jié)果如下:
腳本之家
Gonn
xxxxxxx@163.com
http://www.dhdzp.com
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("點擊確定后將提交表單");
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扁平化需要一個插件:http://www.json.org/json2.js,通過JSON.stringify(str)將對象扁平化然后傳送給php。
注:另有一個http://www.json.org/json.js,對應(yīng)的是toJSONString方法。
var last=obj.toJSONString(); //針對json.js
var last=JSON.stringify(obj); //針對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()這個函數(shù),然后調(diào)用其中數(shù)據(jù)用 $obj->屬性即可。
簡而論之,不管是xml還是json都是為了方便在客戶端與服務(wù)器端交互數(shù)據(jù)的中轉(zhuǎn)站,特別是用于對象型數(shù)據(jù),比如最常見的數(shù)組。
下面將分別將數(shù)組從php傳送給javascript,以及將數(shù)組從javascript傳送給php示例說明,例子比較簡單,明白概念即可。不管從php傳送給javascript,還是javascript傳送給php,json在傳送之前都會將對象扁平化即一維化為字符串。
PHP 向 JavaScript 傳值
PHP 文件 json.php
復(fù)制代碼 代碼如下:
<?php
$arr = array(
'name' => '腳本之家',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.dhdzp.com',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
光執(zhí)行這個文件,其結(jié)果如下:
復(fù)制代碼 代碼如下:
getProfile({"name":"u5e0cu4e9a","nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.dhdzp.com"}})
json.php 是通過 json_encode 函數(shù)將數(shù)組扁平化,然后發(fā)送,相反有個 json_decode 函數(shù)。
那么在 JavaScript 如何調(diào)用呢?很簡單,定義一個變量獲取 PHP 傳來的 Json,該 Json 具備對象的特性,我們可以用 array.name 這種方式來獲取該 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>
運行結(jié)果如下:
復(fù)制代碼 代碼如下:
腳本之家
Gonn
xxxxxxx@163.com
http://www.dhdzp.com
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("點擊確定后將提交表單");
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扁平化需要一個插件:http://www.json.org/json2.js,通過JSON.stringify(str)將對象扁平化然后傳送給php。
注:另有一個http://www.json.org/json.js,對應(yīng)的是toJSONString方法。
復(fù)制代碼 代碼如下:
var last=obj.toJSONString(); //針對json.js
var last=JSON.stringify(obj); //針對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()這個函數(shù),然后調(diào)用其中數(shù)據(jù)用 $obj->屬性即可。
您可能感興趣的文章:
- PHP的Laravel框架結(jié)合MySQL與Redis數(shù)據(jù)庫的使用部署
- CentOS 安裝 PHP5.5+Redis+XDebug+Nginx+MySQL全紀錄
- PHP使用redis實現(xiàn)統(tǒng)計緩存mysql壓力的方法
- docker搭建php+nginx+swoole+mysql+redis環(huán)境的方法
- php中關(guān)于codeigniter的xmlrpc的類在進行數(shù)據(jù)交換時的類型問題
- PHP+Redis開發(fā)的書簽案例實戰(zhàn)詳解
- php結(jié)合redis實現(xiàn)高并發(fā)下的搶購、秒殺功能的實例
- php+redis實現(xiàn)商城秒殺功能
- php+redis消息隊列實現(xiàn)搶購功能
- PHP結(jié)合Redis+MySQL實現(xiàn)冷熱數(shù)據(jù)交換應(yīng)用案例詳解
相關(guān)文章
mysql 中InnoDB和MyISAM的區(qū)別分析小結(jié)
InnoDB和MyISAM是在使用MySQL最常用的兩個表類型,各有優(yōu)缺點,視具體應(yīng)用而定。基本的差別為:MyISAM類型不支持事務(wù)處理等高級處理,而InnoDB類型支持。MyISAM類型的表強調(diào)的是性能,其執(zhí)行數(shù)度比InnoDB類型更快,但是不提供事務(wù)支持,而InnoDB提供事務(wù)支持已經(jīng)外部鍵等高級數(shù)據(jù)庫功能。2008-04-04
PHP和Java 集成開發(fā)詳解分析 強強聯(lián)合
很久以前,有人從www上看到看到天空上一個很亮的亮點,它就是Java語言,與此同時,在另一個地方一位夢想家也看到了一個亮點,它就是PHP.2008-11-11

