首頁  >  文章  >  後端開發  >  PHP如何讓json_encode不轉義中文?

PHP如何讓json_encode不轉義中文?

Guanhui
Guanhui原創
2020-07-22 09:32:286717瀏覽

PHP如何讓json_encode不轉義中文?

PHP如何讓json_encode不轉義中文?

PHP讓json_encode不轉義中文的方法:在使用“json_encode()”函數進行JSON編碼時,在第2個參數傳入常數“JSON_UNESCAPED_UNICODE”,其意義是以字面編碼多位元組Unicode字元。

程式碼範例

$jsonStr = json_encode($data,JSON_UNESCAPED_UNICODE);

JSON 預先定義常數

JSON_ERROR_NONE (integer)

沒有錯誤發生。自 PHP 5.3.0 起生效。

JSON_ERROR_DEPTH (integer)

到達了最大堆疊深度。自 PHP 5.3.0 起生效。

JSON_ERROR_STATE_MISMATCH (integer)

出現了下溢(underflow)或模式不符。自 PHP 5.3.0 起生效。

JSON_ERROR_CTRL_CHAR (integer)

控製字元錯誤,可能是編碼不對。自 PHP 5.3.0 起生效。

JSON_ERROR_SYNTAX (integer)

語法錯誤。自 PHP 5.3.0 起生效。

JSON_ERROR_UTF8 (integer)

異常的 UTF-8 字符,也許是因為不正確的編碼。自 PHP 5.3.3 起生效。

JSON_ERROR_RECURSION (integer)

The object or array passed to json_encode() include recursive references and cannot be encoded. If the JSON_PARTIAL_PUT_ON_ERROR reference. Available since 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_OUT UT_PARTIbe the place of these special numbers. Available since PHP 5.5.0.

JSON_ERROR_UNSUPPORTED_TYPE (integer)

#A value of an unsupported type was given to json_

#A value of an unsupported type was given to json_encode(), resource the as a JSON_PARTIAL_OUTPUT_ON_ERROR option was given, NULL will be encoded in the place of the unsupported value. Available since PHP 5.5.0.

JSON_ERROR_INVALID_PROPERPER 5.5.0.

JSON_ERROR_INVALID_PROPERPER_N string passed to json_decode() when decoding a JSON object into a PHP object. Available since PHP 7.0.0.

##JSON_ERROR_UTF16 (integer)

#S unpaired UTFingle-16 surrogate the JSON string passed to json_encode(). Available since PHP 7.0.0.

下面的常數可以和json_decode() 的form 選項結合使用。

JSON_BIGINT_AS_STRING (integer)

將大數字編碼成原始字元原來的值。自 PHP 5.4.0 起生效。

JSON_OBJECT_AS_ARRAY (integer)

Decodes JSON objects as PHP array. This option can be added automatically by calling json_decode() with the second parameter equal to TR. Available.

##下面的常數可以和json_encode() 的form 選項結合使用。

JSON_HEX_TAG (integer)

所有的 92d33664ec5f827b86e068a341370a86 轉換成 \u003C 和 \u003E。自 PHP 5.3.0 起生效。

JSON_HEX_AMP (integer)

所有的 & 轉換成 \u0026。自 PHP 5.3.0 起生效。

JSON_HEX_APOS (integer)

所有的 ' 轉換成 \u0027。自 PHP 5.3.0 起生效。

JSON_HEX_QUOT (integer)

所有的" 轉換成\u0022。自PHP 5.3.0 起生效。

JSON_FORCE_OBJECT (integer)

#使一個使一個非關聯陣列輸出一個類別(Object)而非陣列。在陣列為空而接受者需要一個類別(Object)的時候尤其有用。自PHP 5.3.0 起生效。

JSON_NUMERIC_CHECK (integer)

將所有數字字串編碼成數字(numbers)。自PHP 5.3.3 起生效。

JSON_PRETTY_PRINT (integer)

用空白字元格式化傳回的資料。自PHP 5.4.0 起生效。

JSON_UNESCAPED_SLASHES (integer)

不要編碼/。 自PHP 5.4.0 起生效。

JSON_UNESCAPED_UNICODE (integer)

以字面編碼多位元組Unicode 字元(預設是編碼成\uXXXX)。自PHP 5.4.0 起生效。

JSON_PARTIAL_OUTPUT_ON_ERROR (integer)

#Substitute some unencodable values instead of failing . Available since PHP 5.5.0.

JSON_PRESERVE_ZERO_FRACTION (integer)

Ensures that float values are always encoded as a float value. Available since PHP 5.6.6 (integer)

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#7.1.#. :《

PHP

以上是PHP如何讓json_encode不轉義中文?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn