{"id":183896,"date":"2024-11-10T08:38:22","date_gmt":"2024-11-10T00:38:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183896\/"},"modified":"2024-11-10T08:38:23","modified_gmt":"2024-11-10T00:38:23","slug":"%e4%bd%bf%e7%94%a8on%e6%a0%bc%e5%bc%8f%e5%82%b3%e8%bc%b8%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-json%e5%bd%a2%e5%bc%8f%e5%82%b3%e8%bc%b8%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183896\/","title":{"rendered":"\u4f7f\u7528ON\u683c\u5f0f\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da (json\u5f62\u5f0f\u50b3\u8f38\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8on%e6%a0%bc%e5%bc%8f%e5%82%b3%e8%bc%b8%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-json%e5%bd%a2%e5%bc%8f%e5%82%b3%e8%bc%b8%e6%95%b8%e6%93%9a%e5%ba%ab-FapjQwsPtz\">\u4f7f\u7528ON\u683c\u5f0f\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da (JSON\u5f62\u5f0f\u50b3\u8f38\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u7684\u50b3\u8f38\u548c\u4ea4\u63db\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da\u662f\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\u4e00\u500b\u6311\u6230\u3002ON\u683c\u5f0f\uff08Object Notation\uff09\u4f5c\u70ba\u4e00\u7a2e\u6578\u64da\u8868\u793a\u683c\u5f0f\uff0c\u7279\u5225\u662fJSON\uff08JavaScript Object Notation\uff09\uff0c\u5728\u9019\u65b9\u9762\u986f\u793a\u51fa\u4e86\u5176\u7368\u7279\u7684\u512a\u52e2\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afjson%e6%a0%bc%e5%bc%8f%ef%bc%9f-FapjQwsPtz\">\u4ec0\u9ebc\u662fJSON\u683c\u5f0f\uff1f<\/h2>\n<p>JSON\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u4ea4\u63db\u683c\u5f0f\uff0c\u6613\u65bc\u4eba\u985e\u95b1\u8b80\u548c\u7de8\u5beb\uff0c\u540c\u6642\u4e5f\u6613\u65bc\u6a5f\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u57fa\u65bcJavaScript\u8a9e\u6cd5\uff0c\u4f46\u7368\u7acb\u65bc\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7de8\u7a0b\u8a9e\u8a00\u4e2d\u3002JSON\u7684\u57fa\u672c\u7d50\u69cb\u7531\u9375\u503c\u5c0d\u7d44\u6210\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87json%e6%a0%bc%e5%bc%8f%ef%bc%9f-FapjQwsPtz\">\u70ba\u4ec0\u9ebc\u9078\u64c7JSON\u683c\u5f0f\uff1f<\/h2>\n<ul>\n<li><strong>\u6613\u65bc\u7406\u89e3\uff1a<\/strong>JSON\u7684\u8a9e\u6cd5\u7c21\u55ae\u660e\u4e86\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<li><strong>\u8f15\u91cf\u7d1a\uff1a<\/strong>\u76f8\u6bd4\u65bcXML\u7b49\u5176\u4ed6\u6578\u64da\u683c\u5f0f\uff0cJSON\u7684\u6578\u64da\u91cf\u66f4\u5c0f\uff0c\u50b3\u8f38\u901f\u5ea6\u66f4\u5feb\u3002<\/li>\n<li><strong>\u5ee3\u6cdb\u652f\u6301\uff1a<\/strong>\u5e7e\u4e4e\u6240\u6709\u7684\u7de8\u7a0b\u8a9e\u8a00\u90fd\u652f\u6301JSON\u683c\u5f0f\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u4e0d\u540c\u7cfb\u7d71\u4e4b\u9593\u7684\u6578\u64da\u4ea4\u63db\u8b8a\u5f97\u66f4\u52a0\u65b9\u4fbf\u3002<\/li>\n<li><strong>\u7d50\u69cb\u5316\u6578\u64da\uff1a<\/strong>JSON\u80fd\u5920\u6709\u6548\u5730\u8868\u793a\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5d4c\u5957\u5c0d\u8c61\u548c\u6578\u7d44\uff0c\u9019\u5c0d\u65bc\u6578\u64da\u5eab\u6578\u64da\u7684\u50b3\u8f38\u5c24\u70ba\u91cd\u8981\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8json%e6%a0%bc%e5%bc%8f%e5%82%b3%e8%bc%b8%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-FapjQwsPtz\">\u5982\u4f55\u4f7f\u7528JSON\u683c\u5f0f\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da\u7684\u904e\u7a0b\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e6%95%b8%e6%93%9a%e6%9f%a5%e8%a9%a2-FapjQwsPtz\">1. \u6578\u64da\u67e5\u8a62<\/h3>\n<p>\u9996\u5148\uff0c\u5f9e\u6578\u64da\u5eab\u4e2d\u67e5\u8a62\u6240\u9700\u7684\u6578\u64da\u3002\u9019\u53ef\u4ee5\u901a\u904eSQL\u8a9e\u53e5\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u7528\u6236\u8868\uff0c\u60f3\u8981\u67e5\u8a62\u6240\u6709\u7528\u6236\u7684\u4fe1\u606f\uff1a<\/p>\n<pre><code>SELECT * FROM users;<\/code><\/pre>\n<h3 id=\"2-%e6%95%b8%e6%93%9a%e8%bd%89%e6%8f%9b-FapjQwsPtz\">2. \u6578\u64da\u8f49\u63db<\/h3>\n<p>\u67e5\u8a62\u7d50\u679c\u901a\u5e38\u662f\u4ee5\u8868\u683c\u7684\u5f62\u5f0f\u8fd4\u56de\u7684\uff0c\u63a5\u4e0b\u4f86\u9700\u8981\u5c07\u9019\u4e9b\u6578\u64da\u8f49\u63db\u70baJSON\u683c\u5f0f\u3002\u8a31\u591a\u7de8\u7a0b\u8a9e\u8a00\u548c\u6846\u67b6\u90fd\u63d0\u4f9b\u4e86\u5c07\u6578\u64da\u8f49\u63db\u70baJSON\u7684\u5167\u5efa\u51fd\u6578\u3002\u4f8b\u5982\uff0c\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import json\nimport sqlite3\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\nconn = sqlite3.connect('example.db')\ncursor = conn.cursor()\n\n# \u67e5\u8a62\u6578\u64da\ncursor.execute(\"SELECT * FROM users;\")\nrows = cursor.fetchall()\n\n# \u5c07\u6578\u64da\u8f49\u63db\u70baJSON\u683c\u5f0f\njson_data = json.dumps(rows)\nprint(json_data)\n\n# \u95dc\u9589\u9023\u63a5\nconn.close()<\/code><\/pre>\n<h3 id=\"3-%e6%95%b8%e6%93%9a%e5%82%b3%e8%bc%b8-FapjQwsPtz\">3. \u6578\u64da\u50b3\u8f38<\/h3>\n<p>\u4e00\u65e6\u6578\u64da\u88ab\u8f49\u63db\u70baJSON\u683c\u5f0f\uff0c\u5c31\u53ef\u4ee5\u901a\u904eHTTP\u8acb\u6c42\u5c07\u5176\u50b3\u8f38\u5230\u5ba2\u6236\u7aef\u6216\u5176\u4ed6\u670d\u52d9\u5668\u3002\u9019\u901a\u5e38\u4f7f\u7528RESTful API\u4f86\u5be6\u73fe\uff0c\u5ba2\u6236\u7aef\u53ef\u4ee5\u901a\u904eGET\u6216POST\u8acb\u6c42\u4f86\u7372\u53d6\u6216\u63d0\u4ea4\u6578\u64da\u3002<\/p>\n<h3 id=\"4-%e6%95%b8%e6%93%9a%e8%a7%a3%e6%9e%90-FapjQwsPtz\">4. \u6578\u64da\u89e3\u6790<\/h3>\n<p>\u5728\u5ba2\u6236\u7aef\u63a5\u6536\u5230JSON\u6578\u64da\u5f8c\uff0c\u9700\u8981\u5c07\u5176\u89e3\u6790\u70ba\u53ef\u7528\u7684\u683c\u5f0f\u3002\u5927\u591a\u6578\u73fe\u4ee3\u7de8\u7a0b\u8a9e\u8a00\u90fd\u63d0\u4f9b\u4e86\u76f8\u61c9\u7684\u5eab\u4f86\u8655\u7406JSON\u6578\u64da\u3002\u4f8b\u5982\uff0c\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>fetch('https:\/\/api.example.com\/users')\n    .then(response =&gt; response.json())\n    .then(data =&gt; console.log(data));<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-FapjQwsPtz\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528ON\u683c\u5f0f\uff08JSON\uff09\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u65b9\u6cd5\u3002\u5b83\u4e0d\u50c5\u7c21\u5316\u4e86\u6578\u64da\u7684\u50b3\u8f38\u904e\u7a0b\uff0c\u9084\u63d0\u9ad8\u4e86\u6578\u64da\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u96a8\u8457\u7db2\u7d61\u61c9\u7528\u7a0b\u5e8f\u7684\u767c\u5c55\uff0c\u638c\u63e1JSON\u683c\u5f0f\u7684\u4f7f\u7528\u5c07\u6210\u70ba\u958b\u767c\u8005\u7684\u4e00\u9805\u91cd\u8981\u6280\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528ON\u683c\u5f0f\u4ee5JSON\u5f62\u5f0f\u9ad8\u6548\u50b3\u8f38\u6578\u64da\u5eab\u6578\u64da\uff0c\u7c21\u5316\u6578\u64da\u4ea4\u4e92\uff0c\u63d0\u5347\u7cfb\u7d71\u6574\u5408\u6027\u8207\u9748\u6d3b\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-183896","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=183896"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183896\/revisions"}],"predecessor-version":[{"id":183897,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183896\/revisions\/183897"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}