PHP JSON

What is JSON?

· JSON refers to JavaScript Object Notation

· JSON is a lightweight text data exchange format

· JSON is language independent *

· JSON is self-describing and easier to understand

* JSON uses JavaScript syntax to describe data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries support many different programming languages.


Environment configuration

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

JSON function


##json_encode

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

Syntax

##json_encode($value,[,options = 0 ] )

Parameter

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

Example


The following example demonstrates How to convert PHP array to JSON format data:


Program running result:

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


The following example demonstrates how to convert PHP objects into JSON format data

name = "sachin"; $e->hobbies = "sports"; $e->birthdate = date('Y-m-d h:i:s a', "2016/9/19 12:20:03 p"); $e->birthdate = date('Y-m-d h:i:s a', strtotime("2016/9/19 12:20:03")); echo json_encode($e); ?>

Program running results:

{"name":"sachin","hobbies":"sports","birthdate":"2016-09-19 12:20:03 pm"} $json [,$assoc = false [, $depth = 512 [, $options = 0 ]]]


json_decode

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

Syntax

json_decode($json [,$assoc = false [, $depth = 512 [, $ options = 0 ]]])

Parameters

##·

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

·

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

·

depth: Integer type parameter, which specifies the recursion depth

·

options: Binary mask, currently only JSON_BIGINT_AS_STRING is supported.


json_decode() common Error

The following three ways of writing json are all wrong. Can you see where the error is?

$bad_json = "{ 'bar': 'baz' }";$bad_json = '{ bar: "baz" }';

$bad_json = '{ "bar": "baz", }';

Executing json_decode() on these three strings will return null and report an error.

The first mistake is that the json delimiter (delimiter) only allows the use of double quotes, not single quotes.

The second mistake is that the "name" (the part to the left of the colon) of the json name-value pair must use double quotes under any circumstances.

The third error is that you cannot add a comma (trailing comma) after the last value.

In addition, json can only be used to represent objects and arrays. If json_decode() is used on a string or value, null will be returned.


Example

The following example demonstrates How to decode JSON data:

Program execution result:

##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)}


##Continuing Learning

||
1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
json_encode json_decode json_last_error · 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· Due to json only accept UTF-8 encoded characters, so the parameters of json_encode() must be UTF-8 encoded, otherwise you will get empty characters or null. When Chinese uses GB2312 encoding, or foreign language uses ISO-8859-1 encoding, special attention should be paid to this point.
 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>
#Function Description
Pair variables Encode JSON
Decode the string in JSON format and convert it into a PHP variable
Return the last error that occurred