English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSON PHP

A common use of JSON is to send data to the web server/Read data from the web server and display data in a web page.

This chapter introduces how to exchange JSON data between the client and PHP server.

PHP JSON functions

PHP5.2version 5.4.0 and above have built-in JSON extension.

PHP has the following built-in functions to handle JSON:

FunctionDescription
json_encode()Returns the JSON representation of the value
json_decode()Parse JSON string
json_last_error()Returns the last error occurred

json_encode() function

PHP functions can be used to convert PHP objects to JSON json_encode().

The given file is json_demo1.php save:

  <?php
  $myObj = new stdClass();
  $myObj->name = "Seagull";
  $myObj->age = 22;
  $myObj->city = "New Delhi";
  $myJSON = json_encode($myObj);  /* Convert PHP object to JSON string */
  
  echo $myJSON;
  ?>

The following example requests a PHP file (json_demo1Then use JSON.parse() to convert the response to a JavaScript object (json_demo

var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
   if (this.readyState ==== 4 && this.status === 200) {
      var myObj = JSON.parse(this.responseText); // Convert JSON string to Object
      document.getElementById("output").innerHTML = myObj.name;
   }
});
httpRequest.open("GET", "json_demo1.php", true);
httpRequest.send();
Test See‹/›

Using PHP functions, PHP arrays can also be converted to JSON json_encode().

The given file is json_demo2.php save:

  <?php
  $myArr = array("Seagull", "Cynthia", "Tarush");
  $myJSON = json_encode($myArr);  /* Convert PHP array to JSON string */
  
  echo $myJSON;
  ?>

The following example is taken from the above example (json_demo2request PHP file, and then use JSON.parse() to convert the response to a JavaScript array:

var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
   if (this.readyState ==== 4 && this.status === 200) {
      var myArr = JSON.parse(this.responseText); // Convert JSON string to array
      document.getElementById("output").innerHTML = myArr[0];
   }
});
httpRequest.open("GET", "json_demo2.php", true);
httpRequest.send();
Test See‹/›

json_decode() function

JSON string can be converted to PHP object using the function json_decode().

The given file is saved by json_demo.php:

  <?php
  $q = $_REQUEST["q"];  // Get q parameter from URL
  $obj = json_decode($q, false); // Convert JSON string to PHP object
  echo $obj->name."  Lives in  ".$obj->city.".";
  ?>

The following example converts a JavaScript object to a JSON string and sends it to the 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;
Test See‹/›

You can use the function to convert JSON string to PHP array json_decode().

<?php
$q = $_REQUEST["q"];  //Get q parameter from URL
$arr = json_decode($q, true); // Convert JSON string to PHP array
echo $arr["name"]."  Lives in  ".$arr["city"].".";
?>