Example of php json-related functions and phpjson Functions
This example describes the usage of php json-related functions. We will share this with you for your reference. The details are as follows:
Function list:
Function |
Description |
Json_encode |
Encode variables in json format |
Json_decode |
Decodes json strings and converts them to php variables. |
Json_last_error |
Returns the last error. |
For example 1: Json_encode
$arr=array("A"=>"a","B"=>"b","C"=>"c","D"=>"d");echo json_encode($arr);
Output:
{"A":"a","B":"b","C":"c","D":"d"}
For example 2: Json_decode
$arr='{"A":"a","B":"b","C":"c","D":"d"}';var_dump(json_decode($arr));var_dump(json_decode($arr,true));
Output:
object(stdClass)[1] public 'A' => string 'a' (length=1) public 'B' => string 'b' (length=1) public 'C' => string 'c' (length=1) public 'D' => string 'd' (length=1)array (size=4) 'A' => string 'a' (length=1) 'B' => string 'b' (length=1) 'C' => string 'c' (length=1) 'D' => string 'd' (length=1)
PS: Here are some useful json online tools for your reference:
Online JSON code verification, validation, beautification, and formatting tools:
Http://tools.jb51.net/code/json
JSON online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson
Json code online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat
C language style/HTML/CSS/json code formatting and beautification tools:
Http://tools.jb51.net/code/ccode_html_css_json