One, the JSON syntax (the $json in Json_decode ($json) in PHP should conform to the JSON syntax format)
①json can represent three kinds of values
1, simple value. Includes integers, string literals, Boolean values, and null. For example: 5, "Hello World", True,null are valid JSON data. Where the JSON string must use double quotes (single quotes can cause syntax errors).
2, object. For example, {"Name": "Tony", "Age": 15}. Description ① requires that attributes must be in double quotes (either without or in single quotes) ② values can be simple values (note that if the value is a string, it must be in double quotes, cannot be in single quotes), or it can be a value (object or array) of complex types.
3, Array. For example, [25,true, "Tony"]. Description: The value of an array can also be of any type.
Second, PHP defines the object literal, does not support direct writing, such as {"A": "One", "B": "22"}. This type of writing is supported in other languages and needs to be defined in PHP as follows:
① $a = ' {' A ': ' One ', ' B ': ' 22 '} '; ' This is a JSON string, and a JSON object without the outside single quotation mark
② $b = Json_decode ($a);
So $b is the literal volume of objects.
Third, the above three numeric types, after Json_encode ($value) conversion, output JSON format (at this point the type is a string), such as "Hello", {},[]. The results of the output can be used in other places (javascrip,ios,java,php, etc.) to be parsed.
Four, the improved Json_encode () and Json_decode () function in PHP, support Chinese (native does not support Chinese)
Note: PHP can not directly rewrite the native function, need to write in the class, as a method of class
The static function Json_encode ($input)
{
//from PHP 5.4.0, adds this option.
if (defined (' Json_unescaped_unicode ')) {return
Json_encode ($input, Json_unescaped_unicode);
}
if (is_string ($input)) {
$text = $input;
$text = str_replace (' \ \ ', ' \\\\ ', $text);
$text = Str_replace (
array ("\ r", "\ n", "T", "\"),
array (' \ r ', ' \ n ', ' \ t ', ' \ \ "),
$text);
Return ' "'. $text. '"';
} else if (Is_array ($input) | | is_object ($input)) {
$arr = array ();
$is _obj = Is_object ($input) | | (Array_keys ($input)!== range (0, COUNT ($input)-1));
foreach ($input as $k => $v) {
if ($v = = null) {
$v = ' null ';
}
if ($is _obj) {
$arr [] = Self::json_encode ($k). ':' . Self::json_encode ($v);
else {
$arr [] = Self::json_encode ($v);
}
}
if ($is _obj) {return
' {'. Join (', ', $arr). '}';
} else {return
' ['. Join (', ', $arr). ']';
}
} else {return
$input. '';
}
The above about PHP in the Json_encode () and Json_decode () function of some of the instructions are small to share the entire content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.