网站首页> 博客> JSON PHP
JSON PHP
PHP 文件
PHP 提供处理 JSON 的内建函数。
通过使用 PHP 函数 json_encode(),PHP 中的对象可转换为 JSON:
name = "Bill Gates";
$myObj->age = 62;
$myObj->city = "Seattle";
$myJSON = json_encode($myObj)
;
echo $myJSON;
?>
客户端 JavaScript
这是客户端上的 JavaScript,使用 AJAX 调用来请求上例的 PHP 文件:
实例
使用 JSON.parse() 把结果转换为 JavaScript 对象:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText)
;
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "demo_file.php", true);
xmlhttp.send();
PHP 数组
在使用 PHP 函数 json_encode() 时,PHP 中的数组也将被转换为 JSON:
PHP 文件
json_encode($myArr); echo $myJSON; ?>
客户端 JavaScript
这是客户端上的 JavaScript,使用 AJAX 调用来请求上例的 PHP 文件:
实例
请使用 JSON.parse() 将结果转换为 JavaScript 数组:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText)
;
document.getElementById("demo").innerHTML = myObj[2];
}
};
xmlhttp.open("GET", "demo_file_array.php", true);
xmlhttp.send();
PHP 数据库
PHP 是服务器端编程语言,应该用于只能由服务器执行的操作,比如访问数据库。
想象一下服务器上有一个数据库,包含客户、产品和供应商数据。
此刻,您需要请求服务器来获取“客户”表中前十条记录:
实例
请使用 JSON.stringify() 将 JavaScript 对象转换为 JSON:
obj = { "table":"customers", "limit":10 };
dbParam = JSON.stringify(obj)
;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "demo_json_db.php?x=" + dbParam, true);
xmlhttp.send();
例子解释:
- 定义包含 table 属性和 limit 属性的对象。
- 将这个对象转换为 JSON 字符串。
- 向这个 PHP 文件发送请求,其中 JSON 作为参数。
- 等待直到请求返回结果(作为 JSON)。
- 显示从 PHP 文件接收到的结果。
查看 PHP 文件
PHP 文件
json_decode($_GET["x"], false);
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$result = $conn->query("SELECT name FROM ".$obj->$table." LIMIT ".$obj->$limit);
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp)
;
?>
PHP 文件解释:
- 将请求转换为对象,使用 PHP 函数 json_decode()。
- 访问数据库,用所请求的数据填充数组。
- 把数组添加到对象,使用 json_encode() 函数以 JSON 返回该对象。
遍历结果
把从 PHP 文件接收到的结果转换为 JavaScript 对象,或者是在本例中的,一个 JavaScript数组:
实例
使用 JSON.parse() 把 JSON 转换为 JavaScript 对象:
... xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myObj =JSON.parse(this.responseText)
; for (x in myObj) { txt += myObj[x].name + "
"; } document.getElementById("demo").innerHTML = txt; } };
PHP 方法 = POST
在向服务器发送数据时,通常最好是使用 HTTP POST 方法。
如需使用 POST 方法来发送 AJAX 请求,请指定该方法和正确的头部。
发送到服务器的数据现在必须是 .send() 方法的参数:
实例
obj = { "table":"customers", "limit":10 }; dbParam = JSON.stringify(obj); xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myObj = JSON.parse(this.responseText); for (x in myObj) { txt += myObj[x].name + "
"; } document.getElementById("demo").innerHTML = txt; } }; xmlhttp.open("POST", "demo_json_db.php", true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send("x=" + dbParam);
PHP 文件中的唯一不同是获取被传输数据的方法。
PHP 文件
使用 $_POST 而不是 $_GET:
query("SELECT name FROM ".$obj->$table." LIMIT ".$obj->$limit); $outp = array(); $outp = $result->fetch_all(MYSQLI_ASSOC); echo json_encode($outp);
?>
- 加入微信群,不定期分享源码和经验

- 签到活跃榜 连续签到送额外金币
- 最新博客
- 校园跑腿系统外卖系统软件平台大学生创业平台搭建 859
- 壹脉销客智能名片CRM系统小程序可二开源码交付部署 883
- 为啥没搞了 1352
- Nginx 的 5 大应用场景,太实用了! 1539
- CentOS 8-stream 安装Postgresql 详细教程 1838
- JAVA智慧校园管理系统小程序源码 电子班牌 Sass 模式 1411
- Java智慧校园系统源码 智慧校园源码 智慧学校源码 智慧校园管理系统源码 小程序+电子班牌 1188
- Java智慧校园系统源码 智慧校园源码 智慧学校源码 智慧校园管理系统源码 小程序+电子班牌 1160
- 致远OA权限 1979
- 发博客会有金币吗 1216