©
This document uses PHP Chinese website manual Release
下列常量由此扩展定义,且仅在此扩展编译入 PHP 或在运行时动态载入时可用。
以下常量表示了 json_last_error() 所返回的错误类型。
JSON_ERROR_NONE
( integer )
JSON_ERROR_DEPTH
( integer )
JSON_ERROR_STATE_MISMATCH
( integer )
JSON_ERROR_CTRL_CHAR
( integer )
JSON_ERROR_SYNTAX
( integer )
JSON_ERROR_UTF8
( integer )
JSON_ERROR_RECURSION
( integer )
The object or array passed to json_encode() include
recursive references and cannot be encoded.
If the JSON_PARTIAL_OUTPUT_ON_ERROR
option was
given, NULL
will be encoded in the place of the recursive reference.
This constant is available as of PHP 5.5.0.
JSON_ERROR_INF_OR_NAN
( integer )
The value passed to json_encode() includes either
NAN
or INF
.
If the JSON_PARTIAL_OUTPUT_ON_ERROR
option was
given, 0 will be encoded in the place of these
special numbers.
This constant is available as of PHP 5.5.0.
JSON_ERROR_UNSUPPORTED_TYPE
( integer )
A value of an unsupported type was given to
json_encode() , such as a resource .
If the JSON_PARTIAL_OUTPUT_ON_ERROR
option was
given, NULL
will be encoded in the place of the unsupported value.
This constant is available as of PHP 5.5.0.
下面的常量可以和 json_encode() 的 form 选项结合使用。
JSON_HEX_TAG
( integer )
JSON_HEX_AMP
( integer )
JSON_HEX_APOS
( integer )
JSON_HEX_QUOT
( integer )
JSON_FORCE_OBJECT
( integer )
JSON_NUMERIC_CHECK
( integer )
JSON_BIGINT_AS_STRING
( integer )
JSON_PRETTY_PRINT
( integer )
JSON_UNESCAPED_SLASHES
( integer )
JSON_UNESCAPED_UNICODE
( integer )
[#1] JuanP [2015-11-24 13:42:08]
Updated format values list:
JSON_HEX_TAG => 1
JSON_HEX_AMP => 2
JSON_HEX_APOS => 4
JSON_HEX_QUOT => 8
JSON_FORCE_OBJECT => 16
JSON_NUMERIC_CHECK => 32
JSON_UNESCAPED_SLASHES => 64
JSON_PRETTY_PRINT => 128
JSON_UNESCAPED_UNICODE => 256
JSON_PARTIAL_OUTPUT_ON_ERROR => 512
JSON_PRESERVE_ZERO_FRACTION => 1024
[#2] e dot grinec at gmail dot com [2015-09-10 04:50:16]
Please, add a constant JSON_FORCE_ARRAY, for associative array force to just array (without keys of array).
[#3] Anonymous [2015-06-13 19:58:59]
I am working with "Marathi Language Data" and I found that when I encoded Marathi data to json using json_encode it returned me unicode values of each character like below:
Program :
<?php
$ar = array("word" => "????");
echo json_encode($ar);
?>
Output of program is :
{"word":"\u0927\u093e\u0921\u0938"}
This is not expected output because what I need is output as
{"word":"????"}
So I used following code:
<?php
$ar = array("word" => "????");
echo json_encode($ar, JSON_UNESCAPED_UNICODE);
?>
And I got the required output as :
{"word":"????"}
[#4] on5wis at mac dot com [2015-04-24 09:27:24]
since json_encode expects a UTF-8 string there is no need to encode the ? symbol.
I suggest you try to echo : json_encode('?');
It gives: "\u20ac"
I'm running PHP 5.6.5 on the CLI, on MacOS X 10.10
[#5] jgarviahonrado at yahoo dot es [2015-04-17 10:06:52]
When decoding the euro symbol (?) as below:
json_encode(utf8_encode('?')
json_encode provides the UTF-8 code \u0080, but it would be really interesting to get the \u20AC one.
Maybe a new option could be added to get the current code or the \u20AC one.
[#6] Yzmir Ramirez [2014-09-23 00:31:53]
If you curious of the numeric values of the constants, as of JSON 1.2.1, the constants have the following values (not that you should use the numbers directly):
JSON_HEX_TAG => 1
JSON_HEX_AMP => 2
JSON_HEX_APOS => 4
JSON_HEX_QUOT => 8
JSON_FORCE_OBJECT => 16
JSON_NUMERIC_CHECK => 32
JSON_UNESCAPED_SLASHES => 64
JSON_PRETTY_PRINT => 128
JSON_UNESCAPED_UNICODE => 256
JSON_ERROR_DEPTH => 1
JSON_ERROR_STATE_MISMATCH => 2
JSON_ERROR_CTRL_CHAR => 3
JSON_ERROR_SYNTAX => 4
JSON_ERROR_UTF8 => 5
JSON_OBJECT_AS_ARRAY => 1
JSON_BIGINT_AS_STRING => 2