复制代码 代码如下:
<?php
$arr = array(
'name' => '脚本之家',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.vevb.com',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
复制代码 代码如下:
getProfile({"name":"u5e0cu4e9a","nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.vevb.com"}})
复制代码 代码如下:
<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></div>
<div></div>
<div></div>
<div></div>
</body>
<script type="text/javascript" src="json.php"></script>
复制代码 代码如下:
脚本之家
Gonn
xxxxxxx@163.com
复制代码 代码如下:
<!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 method="post" action="json_encode.php"onsubmit="JSON_test(this);return flase;">
<label for="txt_name">姓名</label>
<p><input type="text" /></p>
<label for="txt_email">邮箱</label>
<p><input type="text" /></p>
<p><label for="txt_password">密码</label></p>
<p><input type="text" /></p>
<p><input type="text" />
<label for="button"></label>
<input type="submit" value="JSON" />
</p>
</form>
</body>
</html>
复制代码 代码如下:
var last=obj.toJSONString(); //针对json.js
var last=JSON.stringify(obj); //针对json2.js
复制代码 代码如下:
<?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 />';
?>
新闻热点
疑难解答