{"id":47443,"date":"2024-12-02T09:09:11","date_gmt":"2024-12-02T01:09:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47443\/"},"modified":"2024-12-02T09:09:11","modified_gmt":"2024-12-02T01:09:11","slug":"%e5%b7%a7%e5%a6%99%e8%bf%90%e7%94%a8-php-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%8c%e8%a7%a3%e6%9e%90-json-%e6%95%b0%e6%8d%ae%e7%9a%84%e8%89%ba%e6%9c%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47443\/","title":{"rendered":"\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p>\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 JSON \u6570\u636e\uff1a\u63d0\u53d6\u59d3\u540d\uff1a\u4f7f\u7528\u6a21\u5f0f &#8216;\/&#8221;name&#8221;: &#8220;(.+?)&#8221;\/&#8217;\u3002\u63d0\u53d6\u5e74\u9f84\uff1a\u4f7f\u7528\u6a21\u5f0f &#8216;\/&#8221;age&#8221;: (.+?)(?=\\,)|\\Z\/&#8217;\u3002\u63d0\u53d6\u5730\u5740\uff1a\u4f7f\u7528\u6a21\u5f0f &#8216;\/&#8221;address&#8221;: &#8220;(.+?)&#8221;\/&#8217;\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729918237671c751df3113.jpg\" class=\"aligncenter\" title=\"\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f\u63d2\u56fe\" alt=\"\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u67e5\u627e\u3001\u66ff\u6362\u6216\u9a8c\u8bc1\u6587\u672c\u3002\u5728 PHP \u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u6709\u6548\u89e3\u6790 JSON \u6570\u636e\uff0c\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 JSON \u6570\u636e\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a JSON \u6587\u4ef6\u540d\u4e3a <code>data.json<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>{\n  \"name\": \"John Doe\",\n  \"age\": 30,\n  \"address\": \"123 Main Street\"\n}<\/pre>\n<p><strong>\u63d0\u53d6\u59d3\u540d<\/strong><\/p>\n<p>\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u540d\u79f0\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6a21\u5f0f\uff1a<\/p>\n<pre>preg_match('\/\"name\": \"(.+?)\"\/', $jsonStr, $matches);<\/pre>\n<p>\u8fd9\u4e2a\u6a21\u5f0f\u5339\u914d\u4e00\u4e2a\u53cc\u5f15\u53f7\u5305\u56f4\u7684\u540d\u79f0\uff0c\u5176\u4e2d <code>.+?<\/code> \u8868\u793a\u975e\u8d2a\u5a6a\u5339\u914d\u4efb\u4f55\u5b57\u7b26\u5e8f\u5217\uff0c\u76f4\u5230\u4e0b\u4e00\u4e2a\u53cc\u5f15\u53f7\u3002<\/p>\n<p><strong>\u63d0\u53d6\u5e74\u9f84<\/strong><\/p>\n<p>\u8981\u63d0\u53d6\u5e74\u9f84\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6a21\u5f0f\uff1a<\/p>\n<pre>preg_match('\/\"age\": (.+?)(?=\\,)|\\Z\/', $jsonStr, $matches);<\/pre>\n<p>\u8fd9\u4e2a\u6a21\u5f0f\u5339\u914d\u4e00\u4e2a\u6570\u5b57\uff08<code>.+?<\/code>\uff09\uff0c\u540e\u9762\u53ef\u80fd\u6709\u4e00\u4e2a\u9017\u53f7\u6216\u5b57\u7b26\u4e32\u7ed3\u675f\uff08<code>(?=\\,)|\\Z<\/code>\uff09\u3002<\/p>\n<p><strong>\u63d0\u53d6\u5730\u5740<\/strong><\/p>\n<p>\u8981\u63d0\u53d6\u5730\u5740\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6a21\u5f0f\uff1a<\/p>\n<pre>preg_match('\/\"address\": \"(.+?)\"\/', $jsonStr, $matches);<\/pre>\n<p>\u8fd9\u4e2a\u6a21\u5f0f\u4e0e\u540d\u79f0\u6a21\u5f0f\u7c7b\u4f3c\uff0c\u5b83\u5339\u914d\u4e00\u4e2a\u53cc\u5f15\u53f7\u5305\u56f4\u7684\u5730\u5740\u3002<\/p>\n<p><strong>\u5b8c\u6574\u4ee3\u7801<\/strong><\/p>\n<pre>$jsonStr = file_get_contents('data.json');\n\npreg_match('\/\"name\": \"(.+?)\"\/', $jsonStr, $matches);\n$name = $matches[1];\n\npreg_match('\/\"age\": (.+?)(?=\\,)|\\Z\/', $jsonStr, $matches);\n$age = $matches[1];\n\npreg_match('\/\"address\": \"(.+?)\"\/', $jsonStr, $matches);\n$address = $matches[1];\n\necho \"Name: $name&lt;br&gt;\";\necho \"Age: $age&lt;br&gt;\";\necho \"Address: $address&lt;br&gt;\";<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Name: John Doe\nAge: 30\nAddress: 123 Main Street<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6 JSON \u6570\u636e\u4e2d\u7684\u6240\u9700\u4fe1\u606f\uff0c\u4f7f\u4e4b\u6613\u4e8e\u5904\u7406\u548c\u663e\u793a\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f \u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60\u6587\u7ae0\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01 \u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 JSON \u6570\u636e\uff1a\u63d0\u53d6\u59d3\u540d\uff1a\u4f7f\u7528\u6a21\u5f0f &#8216;\/&#8221;name&#8221;: &#8220;(.+?)&#8221;\/&#8217;\u3002\u63d0\u53d6\u5e74\u9f84\uff1a\u4f7f\u7528\u6a21\u5f0f &#8216;\/&#8221;age&#8221;: (.+?)(?=\\,)|\\Z\/&#8217;\u3002\u63d0\u53d6\u5730\u5740\uff1a\u4f7f\u7528\u6a21\u5f0f &#8216;\/&#8221;address&#8221;: &#8220;(.+?)&#8221;\/&#8217;\u3002 \u5de7\u5999\u8fd0\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u89e3\u6790 JSON \u6570\u636e\u7684\u827a\u672f \u7b80\u4ecb \u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u67e5\u627e\u3001\u66ff\u6362\u6216\u9a8c\u8bc1\u6587\u672c\u3002\u5728 PHP \u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u6709\u6548\u89e3\u6790 JSON \u6570\u636e\uff0c\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002 \u5b9e\u6218\u6848\u4f8b \u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790 JSON \u6570\u636e\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a JSON \u6587\u4ef6\u540d\u4e3a data.json\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a { &#8220;name&#8221;: &#8220;John Doe&#8221;, &#8220;age&#8221;: 30, &#8220;address&#8221;: &#8220;123 Main Street&#8221; } \u63d0\u53d6\u59d3\u540d \u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u540d\u79f0\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6a21\u5f0f\uff1a preg_match(&#8216;\/&#8221;name&#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-47443","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47443","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=47443"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47443\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}