该json_encode函数会将一个PHP数组(或者,从PHP 5.4开始,一个实现该JsonSerializable接口的对象)转换为JSON编码的字符串。如果成功,则返回JSON编码的字符串;如果失败,则返回FALSE。
$array = [ 'name' => 'Jeff', 'age' => 20, 'active' => true, 'colors' => ['red', 'blue'], 'values' => [0=>'foo', 3=>'bar'], ];
在编码期间,PHP数据类型string,integer和boolean将转换为它们的JSON等效项。关联数组被编码为JSON对象,并且-当使用默认参数调用时-索引数组被编码为JSON数组。(除非数组键不是从0开始的连续数字序列,在这种情况下,数组将被编码为JSON对象。)
echo json_encode($array);
输出:
{"name":"Jeff","age":20,"active":true,"colors":["red","blue"],"values":{"0":"foo","3":"bar"}}
从PHP 5.3开始,to的第二个参数json_encode是位掩码,可以是以下项中的一个或多个。
与任何位掩码一样,它们可以与二进制OR运算符结合使用|。
强制创建对象而不是数组
$array = ['Joel', 23, true, ['red', 'blue']]; echo json_encode($array); echo json_encode($array, JSON_FORCE_OBJECT);
输出:
["Joel",23,true,["red","blue"]] {"0":"Joel","1":23,"2":true,"3":{"0":"red","1":"blue"}}
在编码过程中确保以下转换:
不变 | 输入值 | 输出结果 |
---|---|---|
JSON_HEX_TAG | < | \u003C |
JSON_HEX_TAG | > | \u003E |
JSON_HEX_AMP | & | \u0026 |
JSON_HEX_APOS | ' | \u0027 |
JSON_HEX_QUOT | " | \u0022 |
$array = ["tag"=>"<>", "amp"=>"&", "apos"=>"'", "quot"=>"\""]; echo json_encode($array); echo json_encode($array, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
输出:
{"tag":"<>","amp":"&","apos":"'","quot":"\""} {"tag":"\u003C\u003E","amp":"\u0026","apos":"\u0027","quot":"\u0022"}
确保将数字字符串转换为整数。
$array = ['23452', 23452]; echo json_encode($array); echo json_encode($array, JSON_NUMERIC_CHECK);
输出:
["23452",23452] [23452,23452]
使JSON易于阅读
$array = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; echo json_encode($array); echo json_encode($array, JSON_PRETTY_PRINT);
输出:
{"a":1,"b":2,"c":3,"d":4} { "a": 1, "b": 2, "c": 3, "d": 4 }
/在输出中包含未转义的正斜杠
$array = ['filename' => 'example.txt', 'path' => '/full/path/to/file/']; echo json_encode($array); echo json_encode($array, JSON_UNESCAPED_SLASHES);
输出:
{"filename":"example.txt","path":"\/full\/path\/to\/file"} {"filename":"example.txt","path":"/full/path/to/file"}
在输出中包括UTF8编码的字符,而不是\u-encoded字符串
$blues = ["english"=>"blue", "norwegian"=>"blå", "german"=>"blau"]; echo json_encode($blues); echo json_encode($blues, JSON_UNESCAPED_UNICODE);
输出:
{"english":"blue","norwegian":"bl\u00e5","german":"blau"} {"english":"blue","norwegian":"blå","german":"blau"}
如果遇到一些无法编码的值,则允许继续编码。
$fp = fopen("foo.txt", "r"); $array = ["file"=>$fp, "name"=>"foo.txt"]; echo json_encode($array); // 没有输出 echo json_encode($array, JSON_PARTIAL_OUTPUT_ON_ERROR);
输出:
{"file":null,"name":"foo.txt"}
确保浮点数始终被编码为浮点数。
$array = [5.0, 5.5]; echo json_encode($array); echo json_encode($array, JSON_PRESERVE_ZERO_FRACTION);
输出:
[5,5.5] [5.0,5.5]
与一起使用时JSON_UNESCAPED_UNICODE,将恢复为较早的PHP版本的行为,并且不会转义字符U + 2028 LINE SEPARATOR和U + 2029 PARAGRAPH SEPARATOR。尽管这些字符在JSON中有效,但在JavaScript中无效,因此的默认行为JSON_UNESCAPED_UNICODE已在7.1版中更改。
$array = ["line"=>"\xe2\x80\xa8", "paragraph"=>"\xe2\x80\xa9"]; echo json_encode($array, JSON_UNESCAPED_UNICODE); echo json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS);
输出:
{"line":"\u2028","paragraph":"\u2029"} {"line":" ","paragraph":" "}