{"id":3075,"date":"2025-03-21T00:12:34","date_gmt":"2025-03-20T16:12:34","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3075"},"modified":"2025-03-21T00:12:34","modified_gmt":"2025-03-20T16:12:34","slug":"php-json-%e5%a4%84%e7%90%86%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/21\/php-json-%e5%a4%84%e7%90%86%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PHP JSON \u5904\u7406\u8be6\u89e3"},"content":{"rendered":"\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e <strong>\u524d\u540e\u7aef\u6570\u636e\u4ea4\u4e92<\/strong>\uff0c\u4f8b\u5982\uff0cWeb \u5e94\u7528\u548c RESTful API \u7684\u901a\u4fe1\u3002PHP \u63d0\u4f9b\u4e86\u5185\u5efa\u7684 <strong>JSON \u6269\u5c55<\/strong>\uff0c\u4f7f\u5f97 PHP \u5728\u5904\u7406 JSON \u6570\u636e\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n\n\n\n<p>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528 JSON\uff0c\u5305\u62ec JSON \u7f16\u7801\u3001\u89e3\u7801\u3001\u9519\u8bef\u5904\u7406\u7b49\u5185\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. JSON \u7f16\u7801\uff08<code>json_encode()<\/code>\uff09<\/strong><\/h2>\n\n\n\n<p><code>json_encode()<\/code> \u51fd\u6570\u5c06 <strong>PHP \u6570\u636e<\/strong>\uff08\u6570\u7ec4\u6216\u5bf9\u8c61\uff09\u8f6c\u6362\u4e3a <strong>JSON \u683c\u5f0f\u5b57\u7b26\u4e32<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u793a\u4f8b\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a JSON<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = &#91;\n    \"name\" =&gt; \"Tom\",\n    \"age\" =&gt; 25,\n    \"city\" =&gt; \"New York\"\n];\n\n$json_data = json_encode($array);\necho $json_data;  \/\/ \u8f93\u51fa: {\"name\":\"Tom\",\"age\":25,\"city\":\"New York\"}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u8fd9\u91cc\u7684 <strong>\u6570\u7ec4<\/strong> \u88ab\u7f16\u7801\u4e3a <strong>JSON \u683c\u5f0f<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a JSON<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person {\n    public $name;\n    public $age;\n\n    function __construct($name, $age) {\n        $this-&gt;name = $name;\n        $this-&gt;age = $age;\n    }\n}\n\n$person = new Person(\"Tom\", 25);\n$json_data = json_encode($person);\necho $json_data;  \/\/ \u8f93\u51fa: {\"name\":\"Tom\",\"age\":25}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u5bf9\u8c61<\/strong> \u4e5f\u80fd\u88ab <code>json_encode()<\/code> \u8f6c\u6362\u4e3a <strong>JSON \u683c\u5f0f\u5b57\u7b26\u4e32<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u63a7\u5236\u9009\u9879\uff08<code>JSON_PRETTY_PRINT<\/code>\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = &#91;\n    \"name\" =&gt; \"Tom\",\n    \"age\" =&gt; 25,\n    \"city\" =&gt; \"New York\"\n];\n\n$json_data = json_encode($array, JSON_PRETTY_PRINT);\necho $json_data;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong><code>JSON_PRETTY_PRINT<\/code><\/strong> \u9009\u9879\u4f7f JSON \u6570\u636e\u66f4\u52a0 <strong>\u53ef\u8bfb<\/strong>\uff0c\u9002\u5408\u8c03\u8bd5\u8f93\u51fa\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. JSON \u89e3\u7801\uff08<code>json_decode()<\/code>\uff09<\/strong><\/h2>\n\n\n\n<p><code>json_decode()<\/code> \u51fd\u6570\u5c06 <strong>JSON \u683c\u5f0f\u5b57\u7b26\u4e32<\/strong> \u8f6c\u6362\u4e3a <strong>PHP \u6570\u636e<\/strong>\uff08\u6570\u7ec4\u6216\u5bf9\u8c61\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u793a\u4f8b\uff1a\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a PHP \u6570\u7ec4<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$json_data = '{\"name\":\"Tom\",\"age\":25,\"city\":\"New York\"}';\n$array = json_decode($json_data, true);\nprint_r($array);<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u8bbe\u7f6e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a <code>true<\/code>\uff0c\u8fd4\u56de\u503c\u4e3a <strong>\u5173\u8054\u6570\u7ec4<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a PHP \u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$json_data = '{\"name\":\"Tom\",\"age\":25,\"city\":\"New York\"}';\n$object = json_decode($json_data);\necho $object-&gt;name;  \/\/ \u8f93\u51fa: Tom<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u5982\u679c\u4e0d\u4f20\u9012\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u9ed8\u8ba4\u8fd4\u56de\u503c\u4e3a <strong>\u5bf9\u8c61<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u9519\u8bef\u5904\u7406\uff08<code>json_last_error()<\/code>\uff09<\/strong><\/h3>\n\n\n\n<p>\u5728\u89e3\u7801 JSON \u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u9519\u8bef\u3002\u901a\u8fc7 <code>json_last_error()<\/code> \u53ef\u4ee5\u83b7\u53d6 <strong>\u89e3\u7801\u9519\u8bef<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$json_data = '{\"name\":\"Tom\", \"age\":25,}';  \/\/ JSON \u683c\u5f0f\u9519\u8bef\uff08\u6700\u540e\u6709\u591a\u4f59\u7684\u9017\u53f7\uff09\n\n$data = json_decode($json_data);\nif (json_last_error() !== JSON_ERROR_NONE) {\n    echo \"JSON \u89e3\u7801\u9519\u8bef\uff1a\" . json_last_error_msg();  \/\/ \u8f93\u51fa: Syntax error\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <code>json_last_error_msg()<\/code> \u8fd4\u56de\u9519\u8bef\u4fe1\u606f\uff0c\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. JSON \u9519\u8bef\u5904\u7406<\/strong><\/h2>\n\n\n\n<p>\u5728\u7f16\u7801\u6216\u89e3\u7801 JSON \u6570\u636e\u65f6\uff0c\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u5e38\u7528\u51fd\u6570\u8fdb\u884c\u9519\u8bef\u68c0\u67e5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u89c1 JSON \u9519\u8bef\u5e38\u91cf<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>JSON_ERROR_NONE<\/code>: \u6ca1\u6709\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>JSON_ERROR_DEPTH<\/code>: \u6700\u5927\u5806\u6808\u6df1\u5ea6\u8d85\u51fa\u9650\u5236\u3002<\/li>\n\n\n\n<li><code>JSON_ERROR_STATE_MISMATCH<\/code>: \u65e0\u6548\u6216\u5f02\u5e38\u7684 JSON \u72b6\u6001\u3002<\/li>\n\n\n\n<li><code>JSON_ERROR_CTRL_CHAR<\/code>: \u63a7\u5236\u5b57\u7b26\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>JSON_ERROR_SYNTAX<\/code>: \u8bed\u6cd5\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>JSON_ERROR_UTF8<\/code>: UTF-8 \u7f16\u7801\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u6355\u83b7 JSON \u7f16\u7801\u9519\u8bef<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$data = &#91;\"name\" =&gt; \"Tom\", \"age\" =&gt; \"\\xB1\\x31\"];  \/\/ \u5305\u542b\u975e\u6cd5\u5b57\u7b26\n\n$json_data = json_encode($data);\nif (json_last_error() !== JSON_ERROR_NONE) {\n    echo \"\u7f16\u7801\u9519\u8bef\uff1a\" . json_last_error_msg();  \/\/ \u8f93\u51fa: Malformed UTF-8 characters, possibly incorrectly encoded\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6570\u636e\u5305\u542b\u975e\u6cd5\u7684 UTF-8 \u5b57\u7b26\uff0c\u56e0\u6b64 <code>json_last_error()<\/code> \u4f1a\u8fd4\u56de\u9519\u8bef\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. JSON \u6570\u636e\u5904\u7406\u7684\u9ad8\u7ea7\u9009\u9879<\/strong><\/h2>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u7f16\u7801\u9009\u9879\uff0c\u5141\u8bb8\u4f60\u66f4\u7cbe\u786e\u5730\u63a7\u5236 JSON \u6570\u636e\u7684\u8f93\u51fa\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u9009\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>JSON_HEX_TAG<\/code><\/strong>: \u5c06 <code>&lt;<\/code> \u548c <code>><\/code> \u8f6c\u6362\u4e3a <code>\\u003C<\/code> \u548c <code>\\u003E<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>JSON_HEX_AMP<\/code><\/strong>: \u5c06 <code>&amp;<\/code> \u8f6c\u6362\u4e3a <code>\\u0026<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>JSON_HEX_APOS<\/code><\/strong>: \u5c06 <code>'<\/code> \u8f6c\u6362\u4e3a <code>\\u0027<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>JSON_HEX_QUOT<\/code><\/strong>: \u5c06 <code>\"<\/code> \u8f6c\u6362\u4e3a <code>\\u0022<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>JSON_FORCE_OBJECT<\/code><\/strong>: \u5f3a\u5236 JSON \u5bf9\u8c61\u7684\u8f93\u51fa\u3002<\/li>\n\n\n\n<li><strong><code>JSON_UNESCAPED_UNICODE<\/code><\/strong>: \u4e0d\u8f6c\u4e49 Unicode \u5b57\u7b26\uff08\u9ed8\u8ba4\u8f6c\u4e49\uff09\u3002<\/li>\n\n\n\n<li><strong><code>JSON_UNESCAPED_SLASHES<\/code><\/strong>: \u4e0d\u8f6c\u4e49\u659c\u6760\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u4e2a\u9009\u9879<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$data = &#91;\n    \"name\" =&gt; \"Tom &amp; Jerry\",\n    \"city\" =&gt; \"New York\"\n];\n\n$json_data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);\necho $json_data;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u8fd9\u91cc\u7684 <code>JSON_UNESCAPED_UNICODE<\/code> \u4f7f\u5f97 <strong>Unicode \u5b57\u7b26\u4e0d\u88ab\u8f6c\u4e49<\/strong>\uff0c<code>JSON_PRETTY_PRINT<\/code> \u5219\u8ba9 JSON \u8f93\u51fa\u66f4\u52a0\u53ef\u8bfb\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u793a\u4f8b\uff1a\u5c06 JSON \u7528\u4e8e AJAX \u8bf7\u6c42<\/strong><\/h2>\n\n\n\n<p>\u5728 Web \u5f00\u53d1\u4e2d\uff0cJSON \u7ecf\u5e38\u4f5c\u4e3a <strong>\u524d\u540e\u7aef\u6570\u636e\u4ea4\u6362<\/strong> \u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u901a\u8fc7 <strong>AJAX<\/strong> \u5411\u670d\u52a1\u5668\u53d1\u9001 JSON \u6570\u636e\uff0c\u5e76\u4ece\u670d\u52a1\u5668\u8fd4\u56de JSON \u683c\u5f0f\u7684\u54cd\u5e94\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u524d\u7aef\uff08JavaScript\uff09\u4ee3\u7801<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let data = {\n    name: \"Tom\",\n    age: 25\n};\n\nfetch(\"process.php\", {\n    method: \"POST\",\n    headers: {\n        \"Content-Type\": \"application\/json\"\n    },\n    body: JSON.stringify(data)\n})\n.then(response =&gt; response.json())\n.then(data =&gt; console.log(data))\n.catch(error =&gt; console.error('Error:', error));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u540e\u7aef\uff08PHP\uff09\u4ee3\u7801\uff08process.php\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u83b7\u53d6 JSON \u6570\u636e\u5e76\u89e3\u7801\n$data = json_decode(file_get_contents(\"php:\/\/input\"), true);\n\n\/\/ \u5904\u7406\u6570\u636e\n$name = $data&#91;\"name\"];\n$age = $data&#91;\"age\"];\n\n\/\/ \u8fd4\u56de JSON \u54cd\u5e94\n$response = &#91;\"status\" =&gt; \"success\", \"name\" =&gt; $name, \"age\" =&gt; $age];\necho json_encode($response);<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7 <strong>AJAX<\/strong> \u5c06 JSON \u6570\u636e\u53d1\u9001\u5230\u670d\u52a1\u5668\uff0c\u540e\u7aef PHP \u89e3\u6790\u8be5\u6570\u636e\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a <strong>JSON \u54cd\u5e94<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>\u51fd\u6570<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td>\u7f16\u7801<\/td><td><code>json_encode()<\/code><\/td><td><code>json_encode([\"name\" =&gt; \"Tom\", \"age\" =&gt; 25])<\/code><\/td><\/tr><tr><td>\u89e3\u7801<\/td><td><code>json_decode()<\/code><\/td><td><code>json_decode('{\"name\":\"Tom\",\"age\":25}')<\/code><\/td><\/tr><tr><td>\u9519\u8bef\u5904\u7406<\/td><td><code>json_last_error()<\/code><\/td><td><code>json_last_error_msg()<\/code><\/td><\/tr><tr><td>\u81ea\u5b9a\u4e49\u9009\u9879<\/td><td><code>JSON_HEX_TAG<\/code>, <code>JSON_PRETTY_PRINT<\/code> \u7b49<\/td><td><code>json_encode($data, JSON_PRETTY_PRINT)<\/code><\/td><\/tr><tr><td>\u53d1\u9001\/\u63a5\u6536 JSON<\/td><td><code>file_get_contents(\"php:\/\/input\")<\/code><\/td><td><code>json_decode(file_get_contents(\"php:\/\/input\"))<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u2705 <strong>PHP JSON \u6269\u5c55\u4f7f\u5f97\u4e0e\u524d\u7aef\u7684\u4ea4\u4e92\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u3001\u5b89\u5168\uff0c\u5e7f\u6cdb\u7528\u4e8e\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\u7684\u6570\u636e\u4f20\u8f93\uff01<\/strong><\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e \u524d\u540e\u7aef\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3075","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3075"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3075\/revisions"}],"predecessor-version":[{"id":3077,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3075\/revisions\/3077"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3076"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}