{"id":46736,"date":"2024-12-02T08:14:29","date_gmt":"2024-12-02T00:14:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46736\/"},"modified":"2024-12-02T08:14:29","modified_gmt":"2024-12-02T00:14:29","slug":"php-%e9%80%92%e5%bd%92%e5%87%bd%e6%95%b0%e5%9c%a8%e8%a7%a3%e6%9e%90-json-%e6%95%b0%e6%8d%ae%e4%b8%ad%e7%9a%84%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46736\/","title":{"rendered":"PHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>\u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u53d1\u6325\u7740\u5173\u952e\u4f5c\u7528\uff0c\u901a\u8fc7\u4f7f\u7528 json_decode() \u51fd\u6570\u548c\u9012\u5f52\u7b97\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u6790\u590d\u6742\u5d4c\u5957\u7684\u6570\u636e\u7ed3\u6784\uff1a\u5b9a\u4e49\u9012\u5f52\u51fd\u6570 parseJSON()\uff0c\u5c06 JSON \u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\u3002\u5bf9 JSON \u7ed3\u679c\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u8fdb\u884c\u8fed\u4ee3\u3002\u5982\u679c\u503c\u662f\u6570\u7ec4\uff0c\u5219\u9012\u5f52\u8c03\u7528 parseJSON()\uff0c\u5c06\u6570\u7ec4\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u5e76\u91cd\u65b0\u89e3\u7801\u3002\u8fd4\u56de\u89e3\u6790\u540e\u7684\u6570\u636e\uff0c\u5176\u4e2d\u6570\u7ec4\u5143\u7d20\u4e5f\u662f\u9012\u5f52\u89e3\u6790\u7684\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729843003671b4f3b06324.jpg\" class=\"aligncenter\" title=\"PHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528\u63d2\u56fe\" alt=\"PHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528<\/strong><\/p>\n<p>\u9012\u5f52\u51fd\u6570\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u89e3\u6790 JSON \u6570\u636e\u65f6\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u9012\u5f52\u5904\u7406 JSON \u6570\u636e\u7684\u51fd\u6570 <code>json_decode()<\/code>\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a JSON \u6587\u4ef6 <code>data.json<\/code>\uff0c\u5305\u542b\u5d4c\u5957\u6570\u7ec4\u548c\u5bf9\u8c61\uff1a<\/p>\n<pre>{\n  \"name\": \"John Doe\",\n  \"age\": 30,\n  \"address\": {\n    \"street\": \"123 Main Street\",\n    \"city\": \"Anytown\",\n    \"state\": \"CA\",\n    \"zip\": \"12345\"\n  },\n  \"hobbies\": [\"reading\", \"writing\", \"coding\"]\n}<\/pre>\n<p><strong>\u9012\u5f52\u89e3\u6790<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u89e3\u6790\u6b64 JSON \u6570\u636e\uff1a<\/p>\n<pre>function parseJSON($json) {\n  $result = json_decode($json, true);\n\n  foreach ($result as $key =&gt; $value) {\n    if (is_array($value)) {\n      $result[$key] = parseJSON(json_encode($value));\n    }\n  }\n\n  return $result;\n}<\/pre>\n<p><strong>\u8c03\u7528\u793a\u4f8b<\/strong><\/p>\n<p>\u8981\u4f7f\u7528\u6b64\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 JSON \u6587\u4ef6\u7684\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\uff1a<\/p>\n<pre>$jsonString = file_get_contents('data.json');\n\n$data = parseJSON($jsonString);<\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u89e3\u6790\u540e\u7684\u6570\u636e\uff1a<\/p>\n<pre>echo $data['name']; \/\/ John Doe\necho $data['address']['street']; \/\/ 123 Main Street\necho implode(', ', $data['hobbies']); \/\/ reading, writing, coding<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u9012\u5f52\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u590d\u6742\u4e14\u5d4c\u5957\u7684 JSON \u6570\u636e\u3002\u8fd9\u4f7f\u5f97\u63d0\u53d6\u548c\u5904\u7406\u6570\u636e\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\uff0c\u4ece\u800c\u4e3a\u5404\u79cd\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528 \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u53d1\u6325\u7740\u5173\u952e\u4f5c\u7528\uff0c\u901a\u8fc7\u4f7f\u7528 json_decode() \u51fd\u6570\u548c\u9012\u5f52\u7b97\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u6790\u590d\u6742\u5d4c\u5957\u7684\u6570\u636e\u7ed3\u6784\uff1a\u5b9a\u4e49\u9012\u5f52\u51fd\u6570 parseJSON()\uff0c\u5c06 JSON \u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\u3002\u5bf9 JSON \u7ed3\u679c\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u8fdb\u884c\u8fed\u4ee3\u3002\u5982\u679c\u503c\u662f\u6570\u7ec4\uff0c\u5219\u9012\u5f52\u8c03\u7528 parseJSON()\uff0c\u5c06\u6570\u7ec4\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u5e76\u91cd\u65b0\u89e3\u7801\u3002\u8fd4\u56de\u89e3\u6790\u540e\u7684\u6570\u636e\uff0c\u5176\u4e2d\u6570\u7ec4\u5143\u7d20\u4e5f\u662f\u9012\u5f52\u89e3\u6790\u7684\u7ed3\u679c\u3002 PHP \u9012\u5f52\u51fd\u6570\u5728\u89e3\u6790 JSON \u6570\u636e\u4e2d\u7684\u4f5c\u7528 \u9012\u5f52\u51fd\u6570\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u89e3\u6790 JSON \u6570\u636e\u65f6\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u9012\u5f52\u5904\u7406 JSON \u6570\u636e\u7684\u51fd\u6570 json_decode()\u3002 \u5b9e\u6218\u6848\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a JSON \u6587\u4ef6 data.json\uff0c\u5305\u542b\u5d4c\u5957\u6570\u7ec4\u548c\u5bf9\u8c61\uff1a { &#8220;name&#8221;: &#8220;John Doe&#8221;, &#8220;age&#8221;: 30, &#8220;address&#8221;: { &#8220;street&#8221;: &#8220;123 Main Street&#8221;, &#8220;city&#8221;: &#8220;Anytown&#8221;, &#8220;state&#8221;: &#8220;CA&#8221;, &#8220;zip&#8221;: &#8220;12345&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-46736","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=46736"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46736\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}