Ô¤¶¨Òå³£Á¿

ÏÂÁг£Á¿ÓÉ´ËÀ©Õ¹¶¨Ò壬ÇÒ½öÔÚ´ËÀ©Õ¹±àÒëÈë PHP »òÔÚÔËÐÐʱ¶¯Ì¬ÔØÈëʱ¿ÉÓá£

ÒÔϳ£Á¿±íʾÁË json_last_error() º¯Êý£¬»ò JsonException ÀàÖÐµÄ code ±äÁ¿Ëù·µ»ØµÄ´íÎóÀàÐÍ¡£

JSON_ERROR_NONE (int)
ûÓдíÎó·¢Éú¡£×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_ERROR_DEPTH (int)
µ½´ïÁË×î´ó¶ÑÕ»Éî¶È¡£×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_ERROR_STATE_MISMATCH (int)
³öÏÖÁËÏÂÒ磨underflow£©»òÕßģʽ²»Æ¥Åä¡£×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_ERROR_CTRL_CHAR (int)
¿ØÖÆ×Ö·û´íÎ󣬿ÉÄÜÊDZàÂë²»¶Ô¡£×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_ERROR_SYNTAX (int)
Óï·¨´íÎó¡£×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_ERROR_UTF8 (int)
Òì³£µÄ UTF-8 ×Ö·û£¬Ò²ÐíÊÇÒòΪ²»ÕýÈ·µÄ±àÂë¡£×Ô PHP 5.3.3 ÆðÉúЧ¡£
JSON_ERROR_RECURSION (int)
´«µÝ¸ø json_encode() º¯ÊýµÄ¶ÔÏó»òÊý×é°üº¬Á˵ݹéÒýÓ㬵¼ÖÂÎÞ·¨±»±àÂë¡£Èç¹û´ò¿ªÁË JSON_PARTIAL_OUTPUT_ON_ERROR Ñ¡ÏÔòÇ£Éæµ½µÝ¹éÒýÓõÄÊý¾Ý»áת»»³É null ºó·µ»Ø¡£×Ô PHP 5.5.0 ÆðÉúЧ¡£
JSON_ERROR_INF_OR_NAN (int)
´«µÝ¸ø json_encode() º¯ÊýµÄ²ÎÊýÖаüº¬ÁË NAN »ò INF£¬µ¼Ö±àÂë³ö´í¡£Èç¹û´ò¿ªÁË JSON_PARTIAL_OUTPUT_ON_ERROR Ñ¡ÏÔòÇ£Éæµ½¶ÔÓ¦²»¿É±àÂëµÄÊý×Ö£¬»áת»»³ÉÊý×Ö 0 ºó·µ»Ø¡£×Ô PHP 5.5.0 ÆðÉúЧ¡£
JSON_ERROR_UNSUPPORTED_TYPE (int)
´«µÝÁ˲»Ö§³ÖµÄÊý¾ÝÀàÐ͸ø json_encode() º¯Êý£¬±ÈÈç ×ÊÔ´(resource)¡£Èç¹û´ò¿ªÁË JSON_PARTIAL_OUTPUT_ON_ERROR Ñ¡ÏÔò¶ÔÓÚ²»Ö§³ÖµÄÊý¾ÝÀàÐÍ£¬»áת»»³É null ºó·µ»Ø¡£×Ô PHP 5.5.0 ÆðÉúЧ¡£
JSON_ERROR_INVALID_PROPERTY_NAME (int)
A key starting with \u0000 character was in the string passed to json_decode() when decoding a JSON object into a PHP object. Available since PHP 7.0.0.
JSON_ERROR_UTF16 (int)
Single unpaired UTF-16 surrogate in unicode escape contained in the JSON string passed to json_encode(). Available since PHP 7.0.0.

ÏÂÃæµÄ³£Á¿¿ÉÒÔºÍ json_decode() µÄ form Ñ¡Ïî½áºÏʹÓá£

JSON_BIGINT_AS_STRING (int)
½«´óÊý×Ö±àÂë³Éԭʼ×Ö·ûÔ­À´µÄÖµ¡£ ×Ô PHP 5.4.0 ÆðÉúЧ¡£
JSON_OBJECT_AS_ARRAY (int)
Decodes JSON objects as PHP array. This option can be added automatically by calling json_decode() with the second parameter equal to true. Available since PHP 5.4.0.

ÏÂÃæµÄ³£Á¿¿ÉÒÔºÍ json_encode() µÄ form Ñ¡Ïî½áºÏʹÓá£

JSON_HEX_TAG (int)
ËùÓÐµÄ < ºÍ > ת»»³É \u003C ºÍ \u003E¡£ ×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_HEX_AMP (int)
ËùÓÐµÄ & ת»»³É \u0026¡£ ×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_HEX_APOS (int)
ËùÓÐµÄ ' ת»»³É \u0027¡£ ×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_HEX_QUOT (int)
ËùÓÐµÄ " ת»»³É \u0022¡£ ×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_FORCE_OBJECT (int)
ʹһ¸ö·Ç¹ØÁªÊý×éÊä³öÒ»¸öÀࣨObject£©¶ø·ÇÊý×é¡£ ÔÚÊý×éΪ¿Õ¶ø½ÓÊÜÕßÐèÒªÒ»¸öÀࣨObject£©µÄʱºòÓÈÆäÓÐÓᣠ×Ô PHP 5.3.0 ÆðÉúЧ¡£
JSON_NUMERIC_CHECK (int)
½«ËùÓÐÊý×Ö×Ö·û´®±àÂë³ÉÊý×Ö£¨numbers£©¡£ ×Ô PHP 5.3.3 ÆðÉúЧ¡£
JSON_PRETTY_PRINT (int)
Óÿհ××Ö·û¸ñʽ»¯·µ»ØµÄÊý¾Ý¡£ ×Ô PHP 5.4.0 ÆðÉúЧ¡£
JSON_UNESCAPED_SLASHES (int)
²»Òª±àÂë /¡£ ×Ô PHP 5.4.0 ÆðÉúЧ¡£
JSON_UNESCAPED_UNICODE (int)
ÒÔ×ÖÃæ±àÂë¶à×Ö½Ú Unicode ×Ö·û£¨Ä¬ÈÏÊDZàÂë³É \uXXXX£©¡£ ×Ô PHP 5.4.0 ÆðÉúЧ¡£
JSON_PARTIAL_OUTPUT_ON_ERROR (int)
Substitute some unencodable values instead of failing. Available since PHP 5.5.0.
JSON_PRESERVE_ZERO_FRACTION (int)
Ensures that float values are always encoded as a float value. Available since PHP 5.6.6.
JSON_UNESCAPED_LINE_TERMINATORS (int)
The line terminators are kept unescaped when JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was before PHP 7.1 without this constant. Available since PHP 7.1.0.

ÏÂÃæµÄ³£Á¿¿ÉÒÔºÍ json_decode() ¼° json_encode() µÄ form Ñ¡Ïî½áºÏʹÓá£

JSON_INVALID_UTF8_IGNORE (int)
Ignore invalid UTF-8 characters. Available as of PHP 7.2.0.
JSON_INVALID_UTF8_SUBSTITUTE (int)
Convert invalid UTF-8 characters to \0xfffd (Unicode Character 'REPLACEMENT CHARACTER') Available as of PHP 7.2.0.
JSON_THROW_ON_ERROR (int)
Throws JsonException if an error occurs instead of setting the global error state that is retrieved with json_last_error() and json_last_error_msg(). JSON_PARTIAL_OUTPUT_ON_ERROR takes precedence over JSON_THROW_ON_ERROR. Available as of PHP 7.3.0.

User Contributed Notes

Anonymous 28-Sep-2018 08:52
flags JSON_NUMERIC_CHECK and JSON_PRESERVE_ZERO_FRACTION are broken in php 7+ — json_encode((float)8.8) returns "8.8000000000000007", and json_encode((float)8.8, JSON_NUMERIC_CHECK) and json_encode((float)8.8, JSON_PRESERVE_ZERO_FRACTION) return "8.8000000000000007" too.

the only way to fix this is setting "serialize_precision = -1" in php.ini
ASchmidt at Anamera dot net 20-May-2018 03:53
In a multi-level array, JSON_FORCE_OBJECT will encode ALL nested numeric arrays as objects.

If your concern was ONLY the first-level array (e.g., to make it suitable as a MySQL JSON column), you could just cast your first-level array to object, e.g.:

<?php
$json
= json_encode( (object) $array, JSON_PRESERVE_ZERO_FRACTION+JSON_UNESCAPED_UNICODE );
?>

Or, if you have large arrays and are concerned about the overhead of object casting, you could append a "null" value beyond the size of the array, which will force the array to become associative:

<?php
$beyond
= count( $array ) + 1;
if ( !
array_key_exists( $beyond, $array) )
    
$array[ $beyond ] = NULL;
$json = json_encode( $array, JSON_PRESERVE_ZERO_FRACTION+JSON_UNESCAPED_UNICODE );
?>

Of course, your later code has to treat an element with a "NULL" value the same as "!isset()", if it iterates the array.
majid4466 at gmail dot com 07-Jul-2016 05:57
To get a really clean json string use these three constants like so:

<?php
$array
= ['€', 'http://example.com/some/cool/page', '337'];
$bad   = json_encode($array);
$good  = json_encode($arrayJSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);

// $bad would be  ["\u20ac","http:\/\/example.com\/some\/cool\/page","337"]
// $good would be ["€","http://example.com/some/cool/page",337]
?>
JuanP 24-Nov-2015 01:42
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
Anonymous 13-Jun-2015 07:58
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":"????"}
on5wis at mac dot com 24-Apr-2015 09:27
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
Yzmir Ramirez 23-Sep-2014 12:31
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