: This article mainly introduces PHP json operations. if you are interested in PHP Tutorial, you can refer to it. Json only accepts UTF-8 characters. Therefore, the json_encode () parameter of the php function must be UTF-8 encoded.
Json format:
======================================
Incorrect json format:
$ Error_json = "{'name': 'Jack'}"; // only double quotation marks are allowed for the json separator, and single quotation marks are not allowed.
$ Error_json = '{name: "jack"}'; // The "key" of the json key-value pair (the left part of the colon). double quotation marks are required.
$ Error_json = '{"name": "baz",}'; // A comma cannot be added after the last value
==================================
Correct json format
$ Yes_json = '{"name": "jack "}';
PHP operations:
(1). json_encode () function: converts arrays and objects to json format.
For example:
① Convert the key/value pair array to json format and convert it to json in object format
$ Arr = array ('a' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5 );
Echo "the arr is:". json_encode ($ arr );
========================================================== ============
After json is converted, the arr is: {"a": 1, "B": 2, "c": 3, "d": 4, "e": 5}
② Convert the index array to json format and convert it to json in array format
$ Arr = Array ('one', 'two', 'Three ');
Echo json_encode ($ arr );
========================================================== ============
["One", "two", "three"]
Forcibly convert an index array into an object:
Json_encode (object) $ arr );
Or
Json_encode ($ arr, JSON_FORCE_OBJECT );
③ Convert the class object to json format and only keep the public field
Class ClassA {
Const ERROR_CODE = '20140901 ';
Public $ public_var = 'This is public_var ';
Private $ private_var = 'This is private_var ';
Protected $ protected_var = 'This is protected_var ';
Public function getErrorCode (){
Return self: ERROR_CODE;
}
}
==========================================================
$ ClassA = new ClassA;
$ ClassA_json = json_encode ($ ClassA );
Echo $ classA_json;
==========================================================
{"Public_var": "this is public_var "}
(2) the json_decode () function converts a json string to a php variable and converts it to an object by default. when the second parameter is set to true, it is converted to a php array.
For example:
①. Convert to php object
$ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5}
Var_dump ($ json );
========================================================== =====
Object (stdClass) #1 (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}
②. Convert to php array
$ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5 }';
Var_dump (json_decode ($ json, true ));
========================================================== =====
Array (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}
The above introduces the PHP json operations, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.