PHP JSON

黄舟
Release: 2023-03-04 11:40:01
Original
996 people have browsed it

In this chapter we will introduce how to use PHP language to encode and decode JSON objects.

Environment configuration

JSON extension has been built-in in php5.2.0 and above.

JSON function

Function

Description

json_encode JSON encoding of variables

json_decode Decodes JSON format strings and converts them into PHP variables

json_last_error Returns the last error that occurred

json_encode

PHP json_encode() is used to encode variables JSON encoding, this function returns JSON data if executed successfully, otherwise it returns FALSE.

Syntax

string json_encode ( $value [, $options = 0 ] )
Copy after login

Parameters

value: The value to be encoded. This function is only valid for UTF-8 encoded data.

options: Binary mask consisting of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT

Examples

The following examples demonstrate how Convert a PHP array to JSON format data:

<?php
   $arr = array(&#39;a&#39; => 1, &#39;b&#39; => 2, &#39;c&#39; => 3, &#39;d&#39; => 4, &#39;e&#39; => 5);
   echo json_encode($arr);
?>
Copy after login

The execution result of the above code is:

{"a":1,"b":2,"c":3,"d":4,"e":5}
Copy after login

The following example demonstrates how to convert a PHP object to JSON format data:

<?php
   class Emp {
       public $name = "";
       public $hobbies  = "";
       public $birthdate = "";
   }
   $e = new Emp();
   $e->name = "sachin";
   $e->hobbies  = "sports";
   $e->birthdate = date(&#39;m/d/Y h:i:s a&#39;, "8/5/1974 12:20:03 p");
   $e->birthdate = date(&#39;m/d/Y h:i:s a&#39;, strtotime("8/5/1974 12:20:03"));

   echo json_encode($e);
?>
Copy after login

The execution result of the above code is:

{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}
Copy after login

json_decode

PHP The json_decode() function is used to decode strings in JSON format and convert them into PHP variables.

Syntax

mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
Copy after login

Parameters

json_string: JSON string to be decoded, must be UTF-8 encoded data

assoc: When this parameter is TRUE When , an array is returned, when FALSE an object is returned.

depth: Integer type parameter, which specifies the recursion depth

options: Binary mask, currently only JSON_BIGINT_AS_STRING is supported.

Example

The following example demonstrates how to decode JSON data:

Copy after login

The execution result of the above code is:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}
Copy after login

The above is the content of PHP JSON, more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Return JSON from PHP script
From 1970-01-01 08:00:00
0
0
0
How to convert json string to json object in php
From 1970-01-01 08:00:00
0
0
0
Extract and access JSON data using PHP
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template