{"id":11,"date":"2023-07-15T12:40:59","date_gmt":"2023-07-15T04:40:59","guid":{"rendered":"http:\/\/www.weigot.com\/?p=11"},"modified":"2023-07-15T12:40:59","modified_gmt":"2023-07-15T04:40:59","slug":"python%e6%8e%a5%e6%94%b6json%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2023\/07\/15\/python%e6%8e%a5%e6%94%b6json%e5%ae%9e%e4%be%8b\/","title":{"rendered":"python\u63a5\u6536JSON\u5b9e\u4f8b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import json\nfrom flask import Flask, request\n\napp = Flask(__name__)\n\n@app.route('\/process_data', methods=&#91;'POST'])\ndef process_data():\n    # \u63a5\u6536 POST \u6570\u636e\n    data = request.get_data()\n\n    # \u5c06 JSON \u6570\u636e\u89e3\u7801\u4e3a\u5b57\u5178\n    postData = json.loads(data)\n\n    # \u68c0\u67e5\u662f\u5426\u6210\u529f\u89e3\u6790 JSON \u6570\u636e\n    if postData is None:\n        # JSON \u89e3\u7801\u5931\u8d25\n        return \"Error: Failed to decode JSON data.\"\n    else:\n        # \u6210\u529f\u89e3\u6790 JSON \u6570\u636e\uff0c\u8fdb\u884c\u5904\u7406\n\n        # \u5c06\u6574\u4e2a JSON \u6570\u636e\u5305\u5199\u5165\u6587\u672c\u6587\u4ef6\n        with open('data.txt', 'a') as file:\n            file.write(json.dumps(postData))\n            file.write(\"\\n\")  # \u5199\u5165\u6362\u884c\u7b26\n\n        # \u8f93\u51fa\u6574\u4e2a JSON \u6570\u636e\u5305\u5230\u63a7\u5236\u53f0\n        print(json.dumps(postData))\n\n        # \u8fd4\u56de\u9002\u5f53\u7684\u54cd\u5e94\uff08\u53ef\u9009\uff09\n        return \"Data received and processed successfully.\"\n\nif __name__ == '__main__':\n    app.run(host='0.0.0.0', port=5000)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":1,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":12,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions\/12"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}