PHP uses JSON and restores JSON to an array of Phpjson, restoring arrays
Before I wrote a simple example of PHP return JSON data, just online, suddenly found an article, also introduced JSON, very detailed, worth reference. The contents are as follows
Starting with version 5.2, PHP native provides the Json_encode () and Json_decode () functions, which are used for encoding, which is used for decoding.
First, Json_encode ()
Copy the Code code as follows:
<?php
$arr = Array (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5);
echo Json_encode ($arr);
?>
Output
Copy the Code code as follows:
{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
Let's look at an example of an object conversion:
Copy the Code code as follows:
$obj->body = ' another post ';
$obj->id = 21;
$obj->approved = true;
$obj->favorite_count = 1;
$obj->status = NULL;
echo Json_encode ($obj);
Output
[/code]
{
"Body": "Another Post",
"id": 21,
"Approved": TRUE,
"Favorite_count": 1,
"Status": null
}
[/code]
Because JSON accepts only utf-8 encoded characters, the Json_encode () argument must be utf-8 encoded, otherwise it will get a null character or null. This is especially important when using GB2312 encoding in Chinese, or when using ISO-8859-1 encoding in a foreign language.
Second, indexed arrays and associative arrays
PHP supports two arrays, one is an indexed array (indexed array) that only holds "value", and the other is an associative array (associative array) that holds "name value pairs" (name/value).
Because JavaScript does not support associative arrays, json_encode () only converts an indexed array (indexed array) into an array format, and the associative array (associative array) to the object format.
For example, there is now an indexed array
Copy the Code code as follows:
$arr = Array (' One ', ' one ', ' one ', ' three ');
echo Json_encode ($arr);
Output
Copy the Code code as follows:
["One", "one", "three"]
If you change it to an associative array:
Copy the Code code as follows:
$arr = Array (' 1 ' = ' one ', ' 2 ' = '-'-', ' 3 ' = ' three ');
echo Json_encode ($arr);
Output becomes
Copy the Code code as follows:
{"1": "One", "2": "One", "3": "Three"}
Note that the data format has changed from "[]" (array) to "{}" (object).
If you need to force an "indexed array" into an "object", you can write
Copy the Code code as follows:
Json_encode ((object) $arr);
Or
Copy the Code code as follows:
Json_encode ($arr, json_force_object);
Iii. Conversion of classes (class)
The following is a PHP class:
Copy the Code code as follows:
Class Foo {
Const Error_code = ' 404 ';
Public $public _EX = ' the public ';
Private $private _EX = ' this is private! ';
protected $protected _EX = ' This should is protected ';
Public Function GetErrorCode () {
return self::error_code;
}
}
Now, JSON transforms the instance of this class:
Copy the Code code as follows:
$foo = new Foo;
$foo _json = Json_encode ($foo);
echo $foo _json;
The output is
Copy the Code code as follows:
{"PUBLIC_EX": "This was public"}
As you can see, other things (constants, private variables, methods, and so on) are lost in addition to the public variables.
Iv. Json_decode ()
This function is used to convert the JSON text to the appropriate PHP data structure. Here is an example:
Copy the Code code as follows:
$json = ' {' foo ': 12345} ';
$obj = Json_decode ($json);
Print $obj->{' foo '}; 12345
Typically, Json_decode () always returns a PHP object instead of an array. Like what:
Copy the Code code as follows:
$json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ';
Var_dump (Json_decode ($json));
The result is to generate a PHP object:
Copy the Code code as follows:
Object (StdClass) #1 (5) {
["a"] = Int (1)
["B"] = + int (2)
["C"] = + int (3)
["D"] = + int (4)
["e"] = + int (5)
}
If you want to force a PHP associative array to be generated, json_decode () needs to add a parameter of true:
Copy the Code code as follows:
$json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ';
Var_dump (Json_decode ($json, true));
As a result, an associative array is generated:
Copy the Code code as follows:
Array (5) {
["a"] = Int (1)
["B"] = + int (2)
["C"] = + int (3)
["D"] = + int (4)
["e"] = + int (5)
}
V. Common errors in Json_decode ()
The following three kinds of JSON notation are wrong, can you see where is wrong?
Copy the Code code as follows:
$bad _json = "{' Bar ': ' Baz '}";
$bad _json = ' {bar: ' Baz '} ';
$bad _json = ' {' bar ': ' Baz ', '} ';
Executing Json_decode () on these three strings will return null and error.
The first error is that the JSON delimiter (delimiter) only allows double quotation marks and cannot use single quotes. The second error is the "name" of the JSON name-value pair (the left part of the colon), which must use double quotes in any case. The third error is that you cannot add a comma after the last value (trailing comma).
In addition, JSON can only be used to represent objects (object) and Arrays (array), and if you use Json_decode () for a string or numeric value, NULL will be returned.
Copy the Code code as follows:
Var_dump (Json_decode ("Hello World")); Null
http://www.bkjia.com/PHPjc/957126.html www.bkjia.com true http://www.bkjia.com/PHPjc/957126.html techarticle PHP uses JSON and restores JSON to an array, Phpjson restore arrays before I wrote a simple instance of PHP return JSON data, just online, suddenly found an article, also introduced JSON, kinda ...