{"id":187874,"date":"2024-11-11T17:54:22","date_gmt":"2024-11-11T09:54:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/187874\/"},"modified":"2024-11-11T17:54:23","modified_gmt":"2024-11-11T09:54:23","slug":"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%bd%89on%e6%a0%bc%e5%bc%8f%ef%bc%9a%e8%ae%93%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e6%9b%b4%e9%9d%88%e6%b4%bb-%e6%95%b8%e6%93%9a%e5%ba%ab%e8%bd%89%e5%8c%96%e7%82%bajson","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/187874\/","title":{"rendered":"\u6578\u64da\u5eab\u8f49ON\u683c\u5f0f\uff1a\u8b93\u6578\u64da\u5b58\u5132\u66f4\u9748\u6d3b (\u6578\u64da\u5eab\u8f49\u5316\u70bajson\u683c\u5f0f\u6587\u4ef6)"},"content":{"rendered":"<h1 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e8%bd%89on%e6%a0%bc%e5%bc%8f%ef%bc%9a%e8%ae%93%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e6%9b%b4%e9%9d%88%e6%b4%bb-KuoIDUsrVS\">\u6578\u64da\u5eab\u8f49ON\u683c\u5f0f\uff1a\u8b93\u6578\u64da\u5b58\u5132\u66f4\u9748\u6d3b<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u7684\u5b58\u5132\u548c\u7ba1\u7406\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0c\u6578\u64da\u5eab\u7684\u683c\u5f0f\u4e5f\u5728\u4e0d\u65b7\u6f14\u8b8a\u3002\u5176\u4e2d\uff0c\u5c07\u6578\u64da\u5eab\u8f49\u5316\u70baJSON\u683c\u5f0f\u6587\u4ef6\u662f\u4e00\u7a2e\u8d8a\u4f86\u8d8a\u53d7\u6b61\u8fce\u7684\u505a\u6cd5\u3002\u9019\u7a2e\u683c\u5f0f\u4e0d\u50c5\u9748\u6d3b\uff0c\u9084\u80fd\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afjson%e6%a0%bc%e5%bc%8f%ef%bc%9f-KuoIDUsrVS\">\u4ec0\u9ebc\u662fJSON\u683c\u5f0f\uff1f<\/h2>\n<p>JSON\uff08JavaScript Object Notation\uff09\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\u7684\u7d50\u69cb\u57fa\u65bc\u9375\u503c\u5c0d\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u7d44\u7e54\u548c\u5b58\u53d6\u8b8a\u5f97\u66f4\u52a0\u76f4\u89c0\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684JSON\u793a\u4f8b\uff1a<\/p>\n<pre><code>{\n  \"name\": \"Server.HK\",\n  \"type\": \"VPS Hosting\",\n  \"location\": \"Hong Kong\"\n}<\/code><\/pre>\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-KuoIDUsrVS\">\u70ba\u4ec0\u9ebc\u9078\u64c7JSON\u683c\u5f0f\uff1f<\/h2>\n<ul>\n<li><strong>\u53ef\u8b80\u6027\uff1a<\/strong>JSON\u683c\u5f0f\u7684\u6578\u64da\u7d50\u69cb\u6e05\u6670\uff0c\u6613\u65bc\u7406\u89e3\uff0c\u9019\u5c0d\u65bc\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u4f86\u8aaa\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>JSON\u652f\u6301\u5d4c\u5957\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u8868\u793a\u8907\u96dc\u7684\u6578\u64da\u95dc\u4fc2\u3002<\/li>\n<li><strong>\u5ee3\u6cdb\u652f\u6301\uff1a<\/strong>\u5e7e\u4e4e\u6240\u6709\u7684\u7de8\u7a0b\u8a9e\u8a00\u548c\u6578\u64da\u5eab\u7cfb\u7d71\u90fd\u652f\u6301JSON\u683c\u5f0f\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u4e00\u500b\u901a\u7528\u7684\u6578\u64da\u4ea4\u63db\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%b0%87%e6%95%b8%e6%93%9a%e5%ba%ab%e8%bd%89%e5%8c%96%e7%82%bajson%e6%a0%bc%e5%bc%8f-KuoIDUsrVS\">\u5982\u4f55\u5c07\u6578\u64da\u5eab\u8f49\u5316\u70baJSON\u683c\u5f0f<\/h2>\n<p>\u5c07\u6578\u64da\u5eab\u8f49\u5316\u70baJSON\u683c\u5f0f\u7684\u904e\u7a0b\u901a\u5e38\u6d89\u53ca\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-KuoIDUsrVS\">1. \u9023\u63a5\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u4f7f\u7528\u9069\u7576\u7684\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\ndb = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"username\",\n    password=\"password\",\n    database=\"database_name\"\n)<\/code><\/pre>\n<h3 id=\"2-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-KuoIDUsrVS\">2. \u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u4e00\u65e6\u9023\u63a5\u6210\u529f\uff0c\u5c31\u53ef\u4ee5\u57f7\u884cSQL\u67e5\u8a62\u4f86\u7372\u53d6\u6240\u9700\u7684\u6578\u64da\uff1a<\/p>\n<pre><code>cursor = db.cursor()\ncursor.execute(\"SELECT * FROM table_name\")\nresults = cursor.fetchall()<\/code><\/pre>\n<h3 id=\"3-%e5%b0%87%e6%95%b8%e6%93%9a%e8%bd%89%e5%8c%96%e7%82%bajson%e6%a0%bc%e5%bc%8f-KuoIDUsrVS\">3. \u5c07\u6578\u64da\u8f49\u5316\u70baJSON\u683c\u5f0f<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684json\u6a21\u584a\u5c07\u67e5\u8a62\u7d50\u679c\u8f49\u5316\u70baJSON\u683c\u5f0f\uff1a<\/p>\n<pre><code>import json\n\n# \u5c07\u7d50\u679c\u8f49\u5316\u70ba\u5b57\u5178\ndata = []\nfor row in results:\n    data.append({\n        \"column1\": row[0],\n        \"column2\": row[1],\n        # \u6dfb\u52a0\u66f4\u591a\u5217\n    })\n\n# \u5c07\u5b57\u5178\u8f49\u5316\u70baJSON\u683c\u5f0f\njson_data = json.dumps(data, ensure_ascii=False, indent=4)\n\n# \u8f38\u51faJSON\u6578\u64da\nprint(json_data)<\/code><\/pre>\n<h2 id=\"json%e6%a0%bc%e5%bc%8f%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-KuoIDUsrVS\">JSON\u683c\u5f0f\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>\u5c07\u6578\u64da\u5eab\u8f49\u5316\u70baJSON\u683c\u5f0f\u5f8c\uff0c\u53ef\u4ee5\u5728\u591a\u7a2e\u5834\u666f\u4e2d\u4f7f\u7528\u9019\u4e9b\u6578\u64da\uff1a<\/p>\n<ul>\n<li><strong>API\u958b\u767c\uff1a<\/strong>\u8a31\u591aWeb API\u4f7f\u7528JSON\u683c\u5f0f\u4f86\u50b3\u8f38\u6578\u64da\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u4ea4\u4e92\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002<\/li>\n<li><strong>\u6578\u64da\u5206\u6790\uff1a<\/strong>\u5206\u6790\u5e2b\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07JSON\u6578\u64da\u5c0e\u5165\u5230\u6578\u64da\u5206\u6790\u5de5\u5177\u4e2d\u9032\u884c\u9032\u4e00\u6b65\u7684\u5206\u6790\u3002<\/li>\n<li><strong>\u524d\u7aef\u958b\u767c\uff1a<\/strong>\u524d\u7aef\u958b\u767c\u8005\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528JSON\u6578\u64da\u4f86\u6e32\u67d3\u7528\u6236\u754c\u9762\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-KuoIDUsrVS\">\u7e3d\u7d50<\/h2>\n<p>\u5c07\u6578\u64da\u5eab\u8f49\u5316\u70baJSON\u683c\u5f0f\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u7684\u9748\u6d3b\u6027\u548c\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u4fc3\u9032\u6578\u64da\u7684\u5171\u4eab\u548c\u61c9\u7528\u3002\u96a8\u8457\u6280\u8853\u7684\u767c\u5c55\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u4f01\u696d\u9078\u64c7\u9019\u7a2e\u65b9\u5f0f\u4f86\u7ba1\u7406\u548c\u5b58\u5132\u6578\u64da\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684\u696d\u52d9\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u6578\u64da\u5eab\u8f49ON\u683c\u5f0f\uff0c\u5c07\u6578\u64da\u8f49\u5316\u70baJSON\u6587\u4ef6\uff0c\u5be6\u73fe\u66f4\u9748\u6d3b\u7684\u6578\u64da\u5b58\u5132\u8207\u7ba1\u7406\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-187874","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187874","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=187874"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187874\/revisions"}],"predecessor-version":[{"id":187875,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/187874\/revisions\/187875"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=187874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=187874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=187874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}