English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
L'uso comune del JSON è inviare dati al server Web / leggere dati dal server Web e visualizzare i dati nella pagina Web.
Questo capitolo spiega come scambiare dati JSON tra il client e il server PHP.
La versione PHP 5.2.0 o superiore ha già integrato l'estensione JSON.
PHP ha le seguenti funzioni integrate per gestire JSON:
Funzione | Descrizione |
---|---|
json_encode() | Ritorna la rappresentazione JSON del valore |
json_decode() | Analizzare una stringa JSON |
json_last_error() | Ritorna l'ultimo errore verificatosi |
Puoi convertire un oggetto PHP in JSON utilizzando la funzione PHP json_encode().
Il file specificato è salvato come json_demo1.php:
<?php $myObj = new stdClass(); $myObj->name = "Seagull"; $myObj->age = 22; $myObj->city = "New Delhi"; $myJSON = json_encode($myObj); /* Convertire un oggetto PHP in una stringa JSON */ echo $myJSON; ?>
Esempio seguente che richiede un file PHP (json_demo1.php) e utilizza JSON.parse() per convertire la risposta in un oggetto JavaScript:
var httpRequest = new XMLHttpRequest(); httpRequest.onreadystatechange = function() { se (this.readyState === 4 && this.status === 200) { var myObj = JSON.parse(this.responseText); // Convertire una stringa JSON in un oggetto document.getElementById("output").innerHTML = myObj.name; } }; httpRequest.open("GET", "json_demo1.php", true); httpRequest.send();Prova a vedere‹/›
Utilizzando la funzione PHP, anche gli array in PHP vengono convertiti in JSON con json_encode().
Il file specificato è salvato come json_demo2.php:
<?php $myArr = array("Seagull", "Cynthia", "Tarush"); $myJSON = json_encode($myArr); /* Convertire un array PHP in una stringa JSON */ echo $myJSON; ?>
Esempio seguente che richiede un file PHP (json_demo2.php) e utilizza JSON.parse() per convertire la risposta in un array JavaScript:
var httpRequest = new XMLHttpRequest(); httpRequest.onreadystatechange = function() { se (this.readyState === 4 && this.status === 200) { var myArr = JSON.parse(this.responseText); // Convertire la stringa JSON in array document.getElementById("output").innerHTML = myArr[0]; } }; httpRequest.open("GET", "json_demo2.php", true); httpRequest.send();Prova a vedere‹/›
La stringa JSON può essere convertita in oggetto PHP utilizzando la funzione json_decode().
Il file specificato è salvato da json_demo.php:
<?php $q = $_REQUEST["q"]; // Ottenere il parametro q dall'URL $obj = json_decode($q, false); // Convertire la stringa JSON in oggetto PHP echo $obj->name."居住在 ".$obj->city."."; ?>
Esempio seguente: convertire un oggetto JavaScript in stringa JSON e inviarlo al server (demo_json.php):
var myObj = {name: "Seagull", age: 22, city: "New Delhi"}; var myJSON = JSON.stringify(myObj); window.location = "json_demo.php?q=" + myJSON;Prova a vedere‹/›
Puoi usare la funzione function per convertire una stringa JSON in array PHP json_decode().
<?php $q = $_REQUEST["q"]; // Ottenere il parametro q dall'URL $arr = json_decode($q, true); // Convertire la stringa JSON in array PHP echo $arr["name"]."居住在 ".$arr["city"]."."; ?>