Starting with version 5.2, PHP native provides Json_encode () and Json_decode () functions, which are used for encoding and decoding.
One, Json_encode ()
String Json_encode (mixed $value [, int $options = 0]): This function is primarily used to convert arrays and objects into JSON format. $value for the data to be converted, you can have any data type in addition to the resource type.
The function can only accept UTF-8 encoded data, or it will get null characters or NULL.
$arr = Array (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5);
echo Json_encode ($arr);
The output is as follows:
{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
For conversion of objects:
$test->site = ' ido321.com ';
$test->id = 22;
$test->name = ' Dwqs ';
echo Json_encode ($test);
The output is:
{
"Site": "Ido321.com",
"id": 22,
"Name": "Dwqs",
}
Second, encoded array
PHP supports associative and indexed arrays, but because JavaScript does not support associative arrays, json_encode () only converts an indexed array (indexed array) into an array format and an associative array (associative array) into object format.
$arr = Array (' One ', ' two ', ' three ');
echo Json_encode ($arr);
The results are:
["One", "two", "three"]
and for associative arrays
$arr = Array (1=> ' one ', 2=> ' two ', 3=> ' three ');
echo Json_encode ($arr);
Result is
{"1": "One", "2": "Two", "3": "Three"}
Note that the data format becomes "{}" (object) from "[]" (array). If you need to force an "indexed array" into an "object", you can write
$arr = Array (' One ', ' two ', ' three ');
Echo Json_encode ((object) $arr);
The result is
{"0": "One", "1": "Two", "2": "Three"}
There's another way.
Echo Json_encode ($arr, json_force_object);
Output: {"0": "One", "1": "Two", "2": "Three"}
Iii. Conversion to classes
Class Test
{
Const test= "TEST";
Public $name = "Dwqs";
Private $age = 21;
protected $sex = "male";
Public Function Gettest ()
{
return self::test;
}
}
$obj =new Test;
echo Json_encode ($obj);
Output results
{' name ': ' Dwqs '}
In addition to exposing variables (public), other things (constants, private variables, methods, and so on) are missing
iv. Json_decode ()
The function primarily encodes JSON-formatted strings, mixed Json_decode (string $json [, bool $assoc]), accepts a JSON-formatted string and converts it to a PHP variable.
<?php
$json = ' {' A ': 1, "B": 2, "C": 3, "D": 4, "E": 5} ';
Var_dump (Json_decode ($json));
Var_dump (Json_decode ($json, true));
?>
The corresponding output 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)
}
Typically, Json_decode () always returns a PHP object, not an array, and if you want to force a PHP associative array, Json_decode () can return an associative array if you need to add a parameter true.
For JSON decoding, it's easy to make mistakes,
$bad _json = "{' Bar ': ' Baz '}";
$bad _json = ' {bar: ' Baz '} ';
$bad _json = ' {' bar ': ' Baz ',} ';
Performing Json_decode () on these three strings will return null and have an error.
The first error is that the JSON delimiter (delimiter) only allows double quotes and cannot use single quotes. The second error is the name of the JSON name value pair (the part to the left of the colon), which must be used in double quotes in any case. The third error is that a comma (trailing comma) cannot be added after the last value.
In addition, JSON can only be used to represent objects (object) and Arrays (array), and null will be returned if Json_decode () is used for a string or numeric value.