Home>

Before I wrote a simple example of php returning json data,Just went onlineSuddenly found an article,Also introduced json, quite detailed,Worth reference.The content is as follows

Since version 5.2,PHP natively provides json_encode () and json_decode () functions, the former is used for encoding,The latter is used for decoding.

First, json_encode ()

<?php
$arr=array ("a" =>1, "b" =>2, "c" =>3, "d" =>4, "e" =>5);
echo json_encode ($arr);
?>

Output

{"a":1, "b":2, "c":3, "d":4, "e":5}

Look at an example of object conversion:

$obj->body="another post";
$obj->id=21;
$obj->approved=true;
$obj->favorite_count=1;
$obj->status=null;
echo json_encode ($obj);

Output

{
"Body":"another post",
"Id":21,
"Approved":true,
"Favorite_count":1,
"Status":null
}

Since json only accepts UTF-8 encoded characters,So the parameter of json_encode () must be UTF-8 encoding, otherwise it will get null characters or null. When Chinese uses gb2312 encoding, or foreign language uses iso-8859-1 encoding,Pay special attention to this.

Indexed arrays and associative arrays

php supports two types of arrays,One is an indexed array that holds only the "value", and the other is an associative array that holds the "name/value".

Since javascript does not support associative arrays,So json_encode () only converts the indexed array into an array format.The associative array is converted to an object format.

For example, now there is an indexed array

$arr=array ("one", "two", "three");

Echo json_encode ($arr);

Output

["one", "two", "three"]

If you change it to an associative array:

$arr=array ("1" =>"one", "2" =>"two", "3" =>"three");
Echo json_encode ($arr);

Output becomes

{"1":"one", "2":"two", "3":"three"}

Notice that the data format has changed from "[]" (array) to "{}" (object).

If you need to cast "index array" to "object", you can write

json_encode ((object) $arr);

or

json_encode ($arr, json_force_object);

Third, the class (class) conversion

Here is a PHP class:

class foo {

Const error_code="404";

Public $public_ex="this is public";

Private $private_ex="this is private!";

Protected $protected_ex="this should be protected";
Public function geterrorcode () {

Return self ::error_code;

}

}

Now, perform a json conversion on an instance of this class:

$foo=new foo;

$Foo_json=json_encode ($foo);

Echo $foo_json;

The output is

{"public_ex":"this is public"}

can be seen,Except for public variables, everything (constants, private variables, methods, etc.) is missing.

Fourth, json_decode ()

This function is used to convert the json text into the corresponding php data structure.Below is an example:

Json $json="{" foo ":12345}";
$Obj=json_decode ($json);

Print $obj->{"foo"};// 12345

usually,json_decode () always returns a php object, not an array.such as:

$json="{" a ":1," b ":2," c ":3," d ":4," e ":5}";
Var_dump (json_decode ($json));

The result is a php object:

object (stdclass) #1 (5) {

["A"] =>int (1)
["B"] =>int (2)
["C"] =>int (3)
["D"] =>int (4)
["E"] =>int (5)

}

If i want to force the generation of a PHP associative array,json_decode () requires a parameter true:

$json="{" a ":1," b ":2," c ":3," d ":4," e ":5}";
Var_dump (json_decode ($json, true));

The result is an associative array:

array (5) {

["A"] =>int (1)
["B"] =>int (2)
["C"] =>int (3)
["D"] =>int (4)
["E"] =>int (5)

}

Five, common errors of json_decode ()

The following three json writing methods are all wrong.Can you see what went wrong?

$bad_json="{" bar ":" baz "}";

$Bad_json="{bar:" baz "}";

$Bad_json="{" bar ":" baz ",}";

Executing json_decode () on all three strings will return null with an error.

The first error was,The delimiter of json only allows double quotes,You cannot use single quotes.The second mistake is,The "name" of the json name-value pair (the part to the left of the colon) must be double-quoted in any case.The third mistake is,You cannot add a trailing comma after the last value.

In addition, json can only be used to represent objects and arrays. If you use json_decode () on a string or value, it will return null.

var_dump (json_decode ("hello world"));// null
php
  • Previous Simple way to dynamically add events to objects in JS
  • Next jQuery implements the function of loading corresponding content according to the position of the scroll bar
  • Trends