{"id":81458,"date":"2024-10-16T15:58:15","date_gmt":"2024-10-16T07:58:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/81458\/"},"modified":"2024-10-16T15:58:15","modified_gmt":"2024-10-16T07:58:15","slug":"on%e5%b0%8d%e8%b1%a1%e5%ad%98%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e7%b0%a1%e5%96%ae%e6%96%b9%e6%b3%95%e5%88%86%e4%ba%ab-%e5%a6%82%e4%bd%95%e6%8a%8ajson%e5%b0%8d%e8%b1%a1%e5%af%ab%e5%85%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/81458\/","title":{"rendered":"ON\u5c0d\u8c61\u5b58\u5165\u6578\u64da\u5eab\u7684\u7c21\u55ae\u65b9\u6cd5\u5206\u4eab (\u5982\u4f55\u628aJSON\u5c0d\u8c61\u5beb\u5165\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"on%e5%b0%8d%e8%b1%a1%e5%ad%98%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e7%b0%a1%e5%96%ae%e6%96%b9%e6%b3%95%e5%88%86%e4%ba%ab-%e5%a6%82%e4%bd%95%e6%8a%8ajson%e5%b0%8d%e8%b1%a1%e5%af%ab%e5%85%a5-izCNaJRjom\">ON\u5c0d\u8c61\u5b58\u5165\u6578\u64da\u5eab\u7684\u7c21\u55ae\u65b9\u6cd5\u5206\u4eab (\u5982\u4f55\u628aJSON\u5c0d\u8c61\u5beb\u5165\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0cJSON\uff08JavaScript Object Notation\uff09\u5df2\u6210\u70ba\u6578\u64da\u4ea4\u63db\u7684\u4e3b\u8981\u683c\u5f0f\u4e4b\u4e00\u3002\u7531\u65bc\u5176\u8f15\u91cf\u7d1a\u548c\u6613\u65bc\u95b1\u8b80\u7684\u7279\u6027\uff0c\u8a31\u591a\u958b\u767c\u8005\u9078\u64c7\u4f7f\u7528JSON\u4f86\u50b3\u905e\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5c07JSON\u5c0d\u8c61\u5b58\u5165\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u65b9\u6cd5\u548c\u793a\u4f8b\u4ee3\u78bc\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-izCNaJRjom\">\u70ba\u4ec0\u9ebc\u9078\u64c7JSON\u683c\u5f0f\uff1f<\/h2>\n<p>JSON\u683c\u5f0f\u7684\u512a\u52e2\u5728\u65bc\u5176\u7d50\u69cb\u7c21\u55ae\u4e14\u6613\u65bc\u89e3\u6790\u3002\u5b83\u7684\u9375\u503c\u5c0d\u7d50\u69cb\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u8b8a\u5f97\u76f4\u89c0\u3002\u6b64\u5916\uff0c\u8a31\u591a\u73fe\u4ee3\u7684\u6578\u64da\u5eab\u7cfb\u7d71\uff08\u5982MongoDB\u3001PostgreSQL\u7b49\uff09\u90fd\u539f\u751f\u652f\u6301JSON\u683c\u5f0f\uff0c\u9019\u4f7f\u5f97\u5b58\u5132\u548c\u67e5\u8a62JSON\u6578\u64da\u8b8a\u5f97\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<h2 id=\"%e5%b0%87json%e5%b0%8d%e8%b1%a1%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%ad%a5%e9%a9%9f-izCNaJRjom\">\u5c07JSON\u5c0d\u8c61\u5beb\u5165\u6578\u64da\u5eab\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e6%ba%96%e5%82%99%e6%95%b8%e6%93%9a%e5%ba%ab-izCNaJRjom\">1. \u6e96\u5099\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u4e00\u500b\u6578\u64da\u5eab\u4f86\u5b58\u5132JSON\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQL\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u8868\u683c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>CREATE TABLE json_data (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    data JSON NOT NULL\n);<\/code><\/pre>\n<h3 id=\"2-%e8%a7%a3%e6%9e%90json%e5%b0%8d%e8%b1%a1-izCNaJRjom\">2. \u89e3\u6790JSON\u5c0d\u8c61<\/h3>\n<p>\u5728\u5c07JSON\u5c0d\u8c61\u5beb\u5165\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5b83\u662f\u6709\u6548\u7684JSON\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528JavaScript\u89e3\u6790JSON\u5c0d\u8c61\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const jsonObject = {\n    name: \"John Doe\",\n    age: 30,\n    city: \"Hong Kong\"\n};\n\nconst jsonString = JSON.stringify(jsonObject); \/\/ \u5c07\u5c0d\u8c61\u8f49\u63db\u70baJSON\u5b57\u7b26\u4e32<\/code><\/pre>\n<h3 id=\"3-%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-izCNaJRjom\">3. \u5beb\u5165\u6578\u64da\u5eab<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SQL\u8a9e\u53e5\u5c07JSON\u5b57\u7b26\u4e32\u5beb\u5165\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Node.js\u548cMySQL\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const mysql = require('mysql');\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'your_username',\n    password: 'your_password',\n    database: 'your_database'\n});\n\nconst jsonString = JSON.stringify(jsonObject);\n\nconnection.query('INSERT INTO json_data (data) VALUES (?)', [jsonString], (error, results) =&gt; {\n    if (error) throw error;\n    console.log('Data inserted:', results.insertId);\n});\n\nconnection.end();<\/code><\/pre>\n<h2 id=\"%e6%9f%a5%e8%a9%a2json%e6%95%b8%e6%93%9a-izCNaJRjom\">\u67e5\u8a62JSON\u6578\u64da<\/h2>\n<p>\u4e00\u65e6\u60a8\u5c07JSON\u5c0d\u8c61\u5beb\u5165\u6578\u64da\u5eab\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u67e5\u8a62\u9019\u4e9b\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62JSON\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>connection.query('SELECT * FROM json_data', (error, results) =&gt; {\n    if (error) throw error;\n    results.forEach(row =&gt; {\n        console.log('Data:', JSON.parse(row.data)); \/\/ \u5c07JSON\u5b57\u7b26\u4e32\u8f49\u63db\u56de\u5c0d\u8c61\n    });\n});<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-izCNaJRjom\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u78ba\u4fdd\u60a8\u7684\u6578\u64da\u5eab\u652f\u6301JSON\u985e\u578b\u3002<\/li>\n<li>\u5728\u5beb\u5165\u6578\u64da\u4e4b\u524d\uff0c\u6aa2\u67e5JSON\u5c0d\u8c61\u7684\u6709\u6548\u6027\u3002<\/li>\n<li>\u8003\u616e\u6578\u64da\u7684\u5b89\u5168\u6027\uff0c\u907f\u514dSQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-izCNaJRjom\">\u7e3d\u7d50<\/h2>\n<p>\u5c07JSON\u5c0d\u8c61\u5beb\u5165\u6578\u64da\u5eab\u662f\u4e00\u500b\u7c21\u55ae\u800c\u6709\u6548\u7684\u904e\u7a0b\uff0c\u7121\u8ad6\u662f\u4f7f\u7528MySQL\u9084\u662f\u5176\u4ed6\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u901a\u904e\u6b63\u78ba\u7684\u6b65\u9a5f\u548c\u4ee3\u78bc\u793a\u4f8b\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u6578\u64da\u7684\u5b58\u5132\u548c\u67e5\u8a62\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c21\u55ae\u65b9\u6cd5\u5206\u4eab\uff0c\u6559\u4f60\u5982\u4f55\u5c07JSON\u5c0d\u8c61\u5b58\u5165\u6578\u64da\u5eab\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u7ba1\u7406\u8207\u5b58\u5132\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-81458","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81458","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=81458"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81458\/revisions"}],"predecessor-version":[{"id":81459,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81458\/revisions\/81459"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=81458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=81458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=81458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}