{"id":46737,"date":"2024-06-09T16:46:56","date_gmt":"2024-06-09T08:46:56","guid":{"rendered":"http:\/\/www.biocloudservice.com\/wordpress\/?p=46737"},"modified":"2024-06-09T16:49:33","modified_gmt":"2024-06-09T08:49:33","slug":"json_decode%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.biocloudservice.com\/wordpress\/?p=46737","title":{"rendered":"json_decode\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u5728PHP\u4e2d\uff0c`json_decode()` \u51fd\u6570\u7528\u4e8e\u5c06JSON\uff08JavaScript Object Notation\uff09\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPHP\u4e2d\u7684\u76f8\u5e94\u503c\u3002\n\n\u4ee5\u4e0b\u662f `json_decode()` \u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\uff1a\n\n### \u57fa\u672c\u8bed\u6cd5\uff1a\nmixed json_decode ( string $json_string &#91;, bool $assoc &#91;, int $depth = 512 ] ] )\n```\n\n- `$json_string`\uff1a\u8981\u89e3\u7801\u7684JSON\u5b57\u7b26\u4e32\u3002\n- `$assoc`\uff08\u53ef\u9009\uff09\uff1a\u5f53\u8bbe\u7f6e\u4e3a `true` \u65f6\uff0c\u5c06\u8fd4\u56de\u6570\u7ec4\u800c\u4e0d\u662f\u5bf9\u8c61\u3002\u9ed8\u8ba4\u4e3a `false`\u3002\n- `$depth`\uff08\u53ef\u9009\uff09\uff1a\u7528\u4e8e\u63a7\u5236\u9012\u5f52\u6df1\u5ea6\uff0c\u4ee5\u9632\u6b62\u6df1\u5ea6\u8fc7\u5927\u7684JSON\u6570\u636e\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u9ed8\u8ba4\u4e3a512\u3002\n\n### \u793a\u4f8b\uff1a\n\n#### 1. \u5c06JSON\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a\u5bf9\u8c61\uff1a\n```php\n$json_string = '{\"name\": \"Kimi\", \"age\": 1}';\n$object = json_decode($json_string);\nprint_r($object);\n```\n\n#### 2. \u5c06JSON\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a\u5173\u8054\u6570\u7ec4\uff1a\n```php\n$json_string = '{\"name\": \"Kimi\", \"age\": 1}';\n$array = json_decode($json_string, true);\nprint_r($array);\n```\n\n#### 3. \u89e3\u7801JSON\u6570\u7ec4\uff1a\n```php\n$json_string = '&#91;\"Kimi\", \"AI\", \"Assistant\"]';\n$array = json_decode($json_string);\nprint_r($array);\n```\n\n#### 4. \u9519\u8bef\u5904\u7406\uff1a\n\u5982\u679cJSON\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c`json_decode()` \u5c06\u8fd4\u56de `null`\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u503c\u6765\u5904\u7406\u9519\u8bef\u60c5\u51b5\uff1a\n```php\n$json_string = '{\"name\": \"Kimi\", \"age\":}';\n$decoded = json_decode($json_string, true);\n\nif (json_last_error() !== JSON_ERROR_NONE) {\n    echo \"JSON decoding error: \" . json_last_error_msg();\n} else {\n    print_r($decoded);\n}\n```\n\n`json_last_error()` \u548c `json_last_error_msg()` \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u6700\u540e\u4e00\u6b21 `json_decode()` \u8c03\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002\n\n\u4f7f\u7528 `json_decode()` \u51fd\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610fJSON\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5fc5\u987b\u6b63\u786e\uff0c\u5426\u5219\u5c06\u65e0\u6cd5\u6b63\u786e\u89e3\u7801\u3002\u6b64\u5916\uff0c\u5982\u679cJSON\u5b57\u7b26\u4e32\u5305\u542b\u5d4c\u5957\u7684\u5bf9\u8c61\u6216\u6570\u7ec4\uff0c`json_decode()` \u4e5f\u80fd\u591f\u6b63\u786e\u5904\u7406\u3002\n\n\n\u5728PHP\u4e2d\uff0cJSON\u6570\u636e\u672c\u8eab\u662f\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u5b58\u5728\u7684\u3002\u4f46\u662f\uff0c\u5f53\u4f60\u4f7f\u7528 `json_decode()` \u51fd\u6570\u5904\u7406JSON\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3aPHP\u4e2d\u7684\u6570\u7ec4\u6216\u5bf9\u8c61\u3002\n\n\u4ee5\u4e0b\u662f\u4e24\u79cd\u8f6c\u6362\u65b9\u5f0f\uff1a\n\n1. **\u8f6c\u6362\u4e3a\u5bf9\u8c61**\uff08\u9ed8\u8ba4\u60c5\u51b5\uff09\uff1a\n   - \u5982\u679c\u4e0d\u63d0\u4f9b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7ed9 `json_decode()`\uff0c\u6216\u8005\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a `false`\uff0c\u5219JSON\u5b57\u7b26\u4e32\u5c06\u88ab\u89e3\u7801\u4e3a\u4e00\u4e2aPHP\u5bf9\u8c61\u3002\n   ```php\n   $json_string = '{\"name\": \"Kimi\", \"age\": 25}';\n   $object = json_decode($json_string);\n   \/\/ $object \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7 -> \u64cd\u4f5c\u7b26\u8bbf\u95ee\u5c5e\u6027\n   echo $object->name; \/\/ \u8f93\u51fa: Kimi\n   ```\n\n2. **\u8f6c\u6362\u4e3a\u5173\u8054\u6570\u7ec4**\uff1a\n   - \u5982\u679c\u5c06\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a `true`\uff0c\u5219JSON\u5b57\u7b26\u4e32\u5c06\u88ab\u89e3\u7801\u4e3a\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\u3002\n   ```php\n   $json_string = '{\"name\": \"Kimi\", \"age\": 25}';\n   $array = json_decode($json_string, true);\n   \/\/ $array \u662f\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\n   echo $array&#91;'name']; \/\/ \u8f93\u51fa: Kimi\n   ```\n\n\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u539f\u59cb\u7684JSON\u5b57\u7b26\u4e32\u6ca1\u6709\u53d8\uff0c\u5b83\u4ecd\u7136\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\u3002\u53ea\u6709\u901a\u8fc7 `json_decode()` \u51fd\u6570\uff0c\u5b83\u624d\u80fd\u88ab\u8f6c\u6362\u6210PHP\u4e2d\u7684\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u56e0\u6b64\uff0cJSON\u5728PHP\u4e2d\u9996\u5148\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8f6c\u6362\u4e3a\u6570\u7ec4\u6216\u5bf9\u8c61\u3002<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":""},"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/46737"}],"collection":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=46737"}],"version-history":[{"count":3,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/46737\/revisions"}],"predecessor-version":[{"id":46740,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/46737\/revisions\/46740"}],"wp:attachment":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=46737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=46737"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=46737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}