{"id":553,"date":"2024-12-26T22:52:24","date_gmt":"2024-12-26T14:52:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=553"},"modified":"2024-12-26T22:52:24","modified_gmt":"2024-12-26T14:52:24","slug":"%e5%9c%a8-python-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e5%b0%86-json-%e6%95%b0%e6%8d%ae%e4%bb%8e%e5%ad%97%e5%85%b8%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/26\/%e5%9c%a8-python-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e5%b0%86-json-%e6%95%b0%e6%8d%ae%e4%bb%8e%e5%ad%97%e5%85%b8%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6%ef%bc%9f\/","title":{"rendered":"\u5728 Python \u4e2d\uff0c\u5982\u4f55\u5c06 JSON \u6570\u636e\u4ece\u5b57\u5178\u5199\u5165\u6587\u4ef6\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\uff0c\u5c06 JSON \u6570\u636e\u4ece\u5b57\u5178\u5199\u5165\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7 <code>json<\/code> \u6a21\u5757\u8f7b\u677e\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u6b63\u786e\u7684\u65b9\u6cd5\u53ca\u6b65\u9aa4\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5bfc\u5165 <code>json<\/code> \u6a21\u5757<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u786e\u4fdd\u5bfc\u5165 Python \u5185\u7f6e\u7684 <code>json<\/code> \u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u5c06 Python \u5bf9\u8c61\uff08\u5982\u5b57\u5178\uff09\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u51c6\u5907\u5b57\u5178\u6570\u636e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\uff0c\u51c6\u5907\u4e00\u4e2a\u5b57\u5178\u6570\u636e\uff0c\u8fd9\u4e2a\u5b57\u5178\u5c06\u88ab\u5199\u5165 JSON \u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = {\n    \"name\": \"John\",\n    \"age\": 30,\n    \"city\": \"New York\"\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u4f7f\u7528 <code>json.dump()<\/code> \u5c06\u5b57\u5178\u5199\u5165\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>json.dump()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u5178\u6570\u636e\u5199\u5165\u4e00\u4e2a\u6587\u4ef6\u3002\u4f60\u9700\u8981\u63d0\u4f9b\u6587\u4ef6\u5bf9\u8c61\u4ee5\u53ca\u8981\u5199\u5165\u7684\u5b57\u5178\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165\uff08\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff09\nwith open(\"data.json\", \"w\") as json_file:\n    json.dump(data, json_file, indent=4)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>data<\/code>\uff1a\u8981\u5199\u5165\u7684\u5b57\u5178\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>json_file<\/code>\uff1a\u6587\u4ef6\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>indent=4<\/code>\uff1a\u6307\u5b9a JSON \u6570\u636e\u7684\u7f29\u8fdb\uff08\u683c\u5f0f\u5316\u8f93\u51fa\uff09\u3002\u6b64\u53c2\u6570\u4f7f\u5f97 JSON \u6587\u4ef6\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u7f29\u8fdb\u7684\u7a7a\u683c\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u5904\u7406\u7f16\u7801\u95ee\u9898<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5b57\u5178\u4e2d\u5305\u542b\u975e ASCII \u5b57\u7b26\uff0c\u5efa\u8bae\u8bbe\u7f6e <code>ensure_ascii=False<\/code> \u53c2\u6570\uff0c\u4ee5\u4fbf\u6b63\u786e\u5730\u5904\u7406\u548c\u5b58\u50a8\u8fd9\u4e9b\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.json\", \"w\", encoding=\"utf-8\") as json_file:\n    json.dump(data, json_file, ensure_ascii=False, indent=4)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>encoding=\"utf-8\"<\/code>\uff1a\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u65b9\u5f0f\u4e3a UTF-8\uff0c\u786e\u4fdd\u80fd\u591f\u6b63\u786e\u5b58\u50a8 Unicode \u5b57\u7b26\u3002<\/li>\n\n\n\n<li><code>ensure_ascii=False<\/code>\uff1a\u5141\u8bb8\u4fdd\u5b58\u975e ASCII \u5b57\u7b26\uff08\u5982\u4e2d\u6587\u3001\u65e5\u6587\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u793a\u4f8b\uff1a\u5b8c\u6574\u4ee3\u7801<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ndata = {\n    \"name\": \"John\",\n    \"age\": 30,\n    \"city\": \"New York\"\n}\n\n# \u5c06\u5b57\u5178\u5199\u5165 JSON \u6587\u4ef6\nwith open(\"data.json\", \"w\", encoding=\"utf-8\") as json_file:\n    json.dump(data, json_file, ensure_ascii=False, indent=4)\n\nprint(\"JSON \u6570\u636e\u5df2\u6210\u529f\u5199\u5165\u6587\u4ef6\u3002\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u7ed3\u679c\uff1a<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u4f60\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u770b\u5230\u4e00\u4e2a\u540d\u4e3a <code>data.json<\/code> \u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"John\",\n    \"age\": 30,\n    \"city\": \"New York\"\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>json.dump()<\/code> \u65b9\u6cd5\u5c06\u5b57\u5178\u6570\u636e\u5199\u5165 JSON \u6587\u4ef6\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u901a\u8fc7 <code>indent<\/code> \u53c2\u6570\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7f\u5f97 JSON \u6570\u636e\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>ensure_ascii=False<\/code> \u5904\u7406\u975e ASCII \u5b57\u7b26\uff0c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>encoding=\"utf-8\"<\/code> \u786e\u4fdd\u6b63\u786e\u7f16\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\uff0c\u4f60\u5c31\u80fd\u591f\u5728 Python \u4e2d\u6b63\u786e\u5730\u5c06 JSON \u6570\u636e\u4ece\u5b57\u5178\u5199\u5165\u6587\u4ef6\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u5c06 JSON \u6570\u636e\u4ece\u5b57\u5178\u5199\u5165\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7 json \u6a21\u5757\u8f7b\u677e\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u6b63\u786e\u7684\u65b9\u6cd5\u53ca\u6b65\u9aa4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=553"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/553\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}