First download the following file (this is a piece of code that someone else wrote to parse the JSON) and introduce the file!
Http://pan.baidu.com/s/1dD8qVr7
Now when we need to use Ajax to interact with the background, how to transfer the PHP array to the JS file and JS identified?
First look at the php file, when we get to $arr this array
foreach ($arr as $value) {
$json. = Json_encode ($value). ',';
}
Echo ' ['. substr ($json, 0,strlen ($json)-1). ']';
Json_encode () is the JSON encoding of each of the $arr values, and then we want to output a JSON array, so we add a comma after each compiled value and finally add ' [] ' outside all the values, which is the format of the JSON array, Note Because we add a comma after each value is JSON encoded, which causes the last of all values to merge the array with a comma, all of which we have to use the substr () function to remove the last comma!
then we'll look at the JS file .
When we use ARR to accept the JSON array of the PHP file transfer
var json = Json.parse (arr);
JSON is one of the objects we started to download in that file, and we use its Parse method to convert the JSON array into an array of JS! This is the variable JSON is connected to a JS array so you can't print it directly, you can traverse this JSON array or json[0] to output!
In fact, we put the PHP array into a JS array of ideas is to use the middle amount of JSON to achieve! Of course, you can only use PHP and JS to achieve the transformation of the array, more than one way!
Conversion between the PHP array and the JSON
JSON is used most of the time because of the data interaction between the program and the JS function when using AJAX objects. Because JS does not know the array in PHP, PHP does not know the array or object in JS. JSON solves this problem very well.
Introduction to JSON
JSON (JavaScript Object notation) is a lightweight data interchange format. It is based on a subset of JavaScript, which means that JavaScript can read JSON directly, which is very convenient.
The exact form of JSON is:
1. The object
Object is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (a colon), and the ' name/value ' pair is separated by a ', ' (comma).
For example: {"username": "Eric", "Age": "," Sex ":" Mans "}
code example:
<script type= "Text/javascript" > function GetUser ()
{
var user = {"
username": "Eric",
"Age": "
Family": {"mother": "Marry", "Father": "Alon", "Brother": "Tom"}
; alert (user.username); alert (user.age); alert (user.family.brother);
} GetUser (); </script>
2, array
An array is an ordered collection of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma) value.
For example: ["Eric", "Man"]
code example:
<script type= "Text/javascript" > function GetArray ()
{
var arr = ["Jarry", 23, [" Www.xiaophper.com "," wxyh_999@126.com "]];
Alert (arr[0]); Alert (arr[1]); Alert (arr[2][0]);
Alert (arr[2][1]);
}
GetArray ();
</script>
Note: Objects and arrays of two forms in JS are not the same when called, object with "." Called, the array is called with subscript [0], [1]. Also note that string values are enclosed in quotation marks when passing a JSON string.
Converting arrays into JSON in PHP
Powerful PHP already provides built-in functions: Json_encode () and Json_decode (). It's easy to understand that Json_encode () is converting a PHP array into JSON. Instead, Json_decode () converts JSON into a PHP array.
For example:
$array = Array ("name" => "Eric", "Age" =>);
echo Json_encode ($array);
Program will print out:
Copy Code code as follows:
{' name ': ' Eric ', ' age ': 23}
Look at the following example:
$array = Array (0 => "Eric", 1 =>);
echo Json_encode ($array);
Program will print out: ["Eric", 23]
The above two examples show that if the keys of the PHP array are numbers, then Json_encode () returns an array of JSON, if the keys of the PHP array are all strings. Then Json_encode () returns JSON in the form of an object. Just already said. The two in the JS call is different.
In fact, as long as there is a string form key in the key of the PHP array, Json_encode () returns the JSON of the object form. This is not true. Because, although there is no error in the PHP code, but if you pass this JSON to the JS function, JS will use this JSON as an object, and the object is not possible to use the number as the attribute name. That is, JS does not know what this is: user.0.username (in the middle of the number 0)