{"id":166407,"date":"2024-11-05T16:14:52","date_gmt":"2024-11-05T08:14:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166407\/"},"modified":"2024-11-05T16:14:53","modified_gmt":"2024-11-05T08:14:53","slug":"%e5%bf%ab%e9%80%9f%e5%8f%96json%e6%95%b8%e6%93%9a%ef%bc%9ahttp%e6%a1%86%e6%9e%b6%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%a5%91%e5%90%88-http%e6%a1%86%e6%9e%b6%e5%8f%96json%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166407\/","title":{"rendered":"\u5feb\u901f\u53d6json\u6578\u64da\uff1ahttp\u6846\u67b6\u8207\u6578\u64da\u5eab\u7684\u5951\u5408 (http\u6846\u67b6\u53d6json\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%bf%ab%e9%80%9f%e5%8f%96json%e6%95%b8%e6%93%9a%ef%bc%9ahttp%e6%a1%86%e6%9e%b6%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%a5%91%e5%90%88-URGdDrrfNy\">\u5feb\u901f\u53d6JSON\u6578\u64da\uff1aHTTP\u6846\u67b6\u8207\u6578\u64da\u5eab\u7684\u5951\u5408<\/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\u7121\u8ad6\u662f\u524d\u7aef\u9084\u662f\u5f8c\u7aef\u958b\u767c\uff0cJSON\u90fd\u63d0\u4f9b\u4e86\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u8868\u793a\u65b9\u5f0f\uff0c\u4fbf\u65bc\u4eba\u985e\u95b1\u8b80\u548c\u6a5f\u5668\u89e3\u6790\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u901a\u904eHTTP\u6846\u67b6\u5feb\u901f\u7372\u53d6JSON\u6578\u64da\uff0c\u4e26\u5206\u6790\u5176\u8207\u6578\u64da\u5eab\u7684\u5951\u5408\u5ea6\u3002<\/p>\n<h2 id=\"http%e6%a1%86%e6%9e%b6%e6%a6%82%e8%bf%b0-URGdDrrfNy\">HTTP\u6846\u67b6\u6982\u8ff0<\/h2>\n<p>HTTP\u6846\u67b6\u662f\u7528\u65bc\u69cb\u5efaWeb\u61c9\u7528\u7a0b\u5e8f\u7684\u5de5\u5177\uff0c\u80fd\u5920\u7c21\u5316\u8acb\u6c42\u548c\u97ff\u61c9\u7684\u8655\u7406\u3002\u5e38\u898b\u7684HTTP\u6846\u67b6\u5305\u62ecNode.js\u7684Express\u3001Python\u7684Flask\u548cDjango\u3001Ruby\u7684Rails\u7b49\u3002\u9019\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\u4f86\u8655\u7406HTTP\u8acb\u6c42\uff0c\u4e26\u80fd\u5920\u8f15\u9b06\u5730\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6578\u64da\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8express%e6%a1%86%e6%9e%b6%e7%8d%b2%e5%8f%96json%e6%95%b8%e6%93%9a-URGdDrrfNy\">\u4f7f\u7528Express\u6846\u67b6\u7372\u53d6JSON\u6578\u64da<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Node.js\u7684Express\u6846\u67b6\u4f86\u7372\u53d6JSON\u6578\u64da\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>const express = require('express');\nconst app = express();\nconst port = 3000;\n\n\/\/ \u6a21\u64ec\u6578\u64da\u5eab\u6578\u64da\nconst data = [\n    { id: 1, name: 'Alice' },\n    { id: 2, name: 'Bob' }\n];\n\n\/\/ \u5b9a\u7fa9\u8def\u7531\napp.get('\/api\/users', (req, res) =&gt; {\n    res.json(data);\n});\n\n\/\/ \u555f\u52d5\u670d\u52d9\u5668\napp.listen(port, () =&gt; {\n    console.log(`Server running at http:\/\/localhost:${port}`);\n});<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u7c21\u55ae\u7684API\uff0c\u7576\u7528\u6236\u8a2a\u554f `\/api\/users` \u6642\uff0c\u670d\u52d9\u5668\u5c07\u8fd4\u56de\u4e00\u500b\u5305\u542b\u7528\u6236\u6578\u64da\u7684JSON\u5c0d\u8c61\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%a5%91%e5%90%88-URGdDrrfNy\">\u6578\u64da\u5eab\u7684\u5951\u5408<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u901a\u5e38\u5b58\u5132\u5728\u6578\u64da\u5eab\u4e2d\u3002HTTP\u6846\u67b6\u53ef\u4ee5\u901a\u904eORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u5de5\u5177\u6216\u76f4\u63a5\u7684SQL\u67e5\u8a62\u4f86\u8a2a\u554f\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u5c07\u7372\u53d6\u7684\u6578\u64da\u8f49\u63db\u70baJSON\u683c\u5f0f\u8fd4\u56de\u7d66\u5ba2\u6236\u7aef\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8flask%e6%a1%86%e6%9e%b6%e8%88%87sqlalchemy-URGdDrrfNy\">\u4f7f\u7528Flask\u6846\u67b6\u8207SQLAlchemy<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Python\u7684Flask\u6846\u67b6\u548cSQLAlchemy\u4f86\u7372\u53d6JSON\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>from flask import Flask, jsonify\nfrom flask_sqlalchemy import SQLAlchemy\n\napp = Flask(__name__)\napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:\/\/\/users.db'\ndb = SQLAlchemy(app)\n\nclass User(db.Model):\n    id = db.Column(db.Integer, primary_key=True)\n    name = db.Column(db.String(50))\n\n@app.route('\/api\/users', methods=['GET'])\ndef get_users():\n    users = User.query.all()\n    return jsonify([{'id': user.id, 'name': user.name} for user in users])\n\nif __name__ == '__main__':\n    app.run(debug=True)<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u7528\u6236\u6a21\u578b\uff0c\u4e26\u901a\u904eFlask\u7684\u8def\u7531\u4f86\u7372\u53d6\u6240\u6709\u7528\u6236\u7684\u6578\u64da\uff0c\u6700\u7d42\u5c07\u5176\u8f49\u63db\u70baJSON\u683c\u5f0f\u8fd4\u56de\u3002<\/p>\n<h2 id=\"%e6%80%a7%e8%83%bd%e8%80%83%e9%87%8f-URGdDrrfNy\">\u6027\u80fd\u8003\u91cf<\/h2>\n<p>\u5728\u8a2d\u8a08API\u6642\uff0c\u6027\u80fd\u662f\u4e00\u500b\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002\u4f7f\u7528\u7de9\u5b58\u6280\u8853\uff08\u5982Redis\uff09\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u7684\u8b80\u53d6\u901f\u5ea6\uff0c\u6e1b\u5c11\u5c0d\u6578\u64da\u5eab\u7684\u76f4\u63a5\u67e5\u8a62\u3002\u6b64\u5916\uff0c\u5408\u7406\u8a2d\u8a08API\u7684\u7d50\u69cb\u548c\u4f7f\u7528\u5408\u9069\u7684HTTP\u65b9\u6cd5\uff08\u5982GET\u3001POST\u7b49\uff09\u4e5f\u80fd\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-URGdDrrfNy\">\u7e3d\u7d50<\/h2>\n<p>\u5feb\u901f\u7372\u53d6JSON\u6578\u64da\u7684\u904e\u7a0b\u4e2d\uff0cHTTP\u6846\u67b6\u8207\u6578\u64da\u5eab\u7684\u6709\u6548\u7d50\u5408\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u9078\u64c7\u5408\u9069\u7684\u6846\u67b6\u548c\u6578\u64da\u5eab\u6280\u8853\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u69cb\u5efa\u51fa\u9ad8\u6548\u3001\u9748\u6d3b\u7684API\uff0c\u6eff\u8db3\u73fe\u4ee3\u61c9\u7528\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u4f7f\u7528Node.js\u7684Express\u9084\u662fPython\u7684Flask\uff0c\u958b\u767c\u8005\u90fd\u80fd\u5920\u8f15\u9b06\u5730\u5be6\u73fe\u6578\u64da\u7684\u5feb\u901f\u7372\u53d6\u548c\u8655\u7406\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u9805\u76ee\u63d0\u4f9b\u7a69\u5b9a\u7684\u57fa\u790e\u8a2d\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u901a\u904eHTTP\u6846\u67b6\u5feb\u901f\u7372\u53d6JSON\u6578\u64da\uff0c\u4e26\u4e86\u89e3\u5176\u8207\u6578\u64da\u5eab\u7684\u6709\u6548\u5951\u5408\u65b9\u5f0f\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-166407","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166407","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=166407"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166407\/revisions"}],"predecessor-version":[{"id":166408,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166407\/revisions\/166408"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}