{"id":2881,"date":"2025-03-16T12:50:24","date_gmt":"2025-03-16T04:50:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2881"},"modified":"2025-03-16T12:50:24","modified_gmt":"2025-03-16T04:50:24","slug":"python3-json-%e6%95%b0%e6%8d%ae%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-json-%e6%95%b0%e6%8d%ae%e8%a7%a3%e6%9e%90\/","title":{"rendered":"Python3 JSON \u6570\u636e\u89e3\u6790"},"content":{"rendered":"\n<p>\u5728 Python3 \u4e2d\uff0c\u5904\u7406 JSON \u6570\u636e\u975e\u5e38\u7b80\u5355\uff0c\u6807\u51c6\u5e93 <code>json<\/code> \u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684 API \u6765\u89e3\u6790 JSON \u683c\u5f0f\u7684\u6570\u636e\u3002JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e38\u7528\u4e8e Web \u5f00\u53d1\u548c\u6570\u636e\u4f20\u8f93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <code>json<\/code> \u6a21\u5757\u7b80\u4ecb<\/strong><\/h3>\n\n\n\n<p>Python \u7684 <code>json<\/code> \u6a21\u5757\u7528\u4e8e\u7f16\u7801\uff08\u5e8f\u5217\u5316\uff09\u548c\u89e3\u7801\uff08\u53cd\u5e8f\u5217\u5316\uff09JSON \u6570\u636e\u3002\u4f60\u53ef\u4ee5\u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u5c06 JSON \u683c\u5f0f\u7684\u6570\u636e\u8f6c\u6362\u4e3a Python \u5bf9\u8c61\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. JSON \u6570\u636e\u89e3\u6790\uff1a\u4ece\u5b57\u7b26\u4e32\u5230 Python \u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 \u89e3\u6790 JSON \u5b57\u7b26\u4e32<\/strong><\/h4>\n\n\n\n<p>\u8981\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>json.loads()<\/code> \u51fd\u6570\u3002\u5b83\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Python \u5b57\u5178\u6216\u5176\u4ed6\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\n# JSON \u5b57\u7b26\u4e32\njson_data = '{\"name\": \"Alice\", \"age\": 30, \"city\": \"New York\"}'\n\n# \u89e3\u6790 JSON \u5b57\u7b26\u4e32\ndata = json.loads(json_data)\n\n# \u8f93\u51fa\u7ed3\u679c\nprint(data)\nprint(type(data))<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'name': 'Alice', 'age': 30, 'city': 'New York'}\n&lt;class 'dict'&gt;<\/code><\/pre>\n\n\n\n<p><code>json.loads()<\/code> \u8fd4\u56de\u7684\u662f\u4e00\u4e2a Python \u5b57\u5178\uff0c\u4f60\u53ef\u4ee5\u50cf\u64cd\u4f5c\u5b57\u5178\u4e00\u6837\u64cd\u4f5c\u89e3\u6790\u540e\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2 \u89e3\u6790\u5305\u542b\u6570\u7ec4\u7684 JSON \u5b57\u7b26\u4e32<\/strong><\/h4>\n\n\n\n<p>JSON \u4e2d\u4e5f\u53ef\u4ee5\u5305\u542b\u6570\u7ec4\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u89e3\u6790\u5305\u542b\u6570\u7ec4\u7684 JSON \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\n# JSON \u5b57\u7b26\u4e32\uff0c\u5305\u542b\u6570\u7ec4\njson_data = '{\"name\": \"Bob\", \"age\": 25, \"languages\": &#91;\"English\", \"Spanish\", \"French\"]}'\n\n# \u89e3\u6790 JSON \u5b57\u7b26\u4e32\ndata = json.loads(json_data)\n\n# \u8f93\u51fa\u6570\u636e\nprint(data)\nprint(data&#91;\"languages\"])  # \u8f93\u51fa\u6570\u7ec4<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'name': 'Bob', 'age': 25, 'languages': &#91;'English', 'Spanish', 'French']}\n&#91;'English', 'Spanish', 'French']<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. JSON \u6570\u636e\u89e3\u6790\uff1a\u4ece\u6587\u4ef6\u5230 Python \u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c JSON \u6570\u636e\u4fdd\u5b58\u5728\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>json.load()<\/code> \u51fd\u6570\u5c06 JSON \u6587\u4ef6\u5185\u5bb9\u52a0\u8f7d\u5230 Python \u5bf9\u8c61\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.1 \u4ece\u6587\u4ef6\u4e2d\u89e3\u6790 JSON \u6570\u636e<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\n# \u5047\u8bbe JSON \u6570\u636e\u4fdd\u5b58\u5728 'data.json' \u6587\u4ef6\u4e2d\nwith open('data.json', 'r') as file:\n    data = json.load(file)\n\n# \u8f93\u51fa\u89e3\u6790\u540e\u7684\u6570\u636e\nprint(data)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. JSON \u6570\u636e\u7f16\u7801\uff1a\u4ece Python \u5bf9\u8c61\u5230 JSON<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.1 \u7f16\u7801 Python \u5bf9\u8c61\u4e3a JSON \u5b57\u7b26\u4e32<\/strong><\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>json.dumps()<\/code> \u51fd\u6570\u5c06 Python \u5bf9\u8c61\uff08\u5982\u5b57\u5178\u3001\u5217\u8868\uff09\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\n# Python \u5b57\u5178\ndata = {\"name\": \"Charlie\", \"age\": 35, \"city\": \"London\"}\n\n# \u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\njson_string = json.dumps(data)\n\n# \u8f93\u51fa JSON \u5b57\u7b26\u4e32\nprint(json_string)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"name\": \"Charlie\", \"age\": 35, \"city\": \"London\"}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.2 \u8bbe\u7f6e <code>indent<\/code> \u7f8e\u5316 JSON \u8f93\u51fa<\/strong><\/h4>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>indent<\/code> \u53c2\u6570\u6765\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u589e\u52a0\u53ef\u8bfb\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ndata = {\"name\": \"Charlie\", \"age\": 35, \"city\": \"London\"}\n\n# \u683c\u5f0f\u5316\u8f93\u51fa JSON \u5b57\u7b26\u4e32\njson_string = json.dumps(data, indent=4)\n\nprint(json_string)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"Charlie\",\n    \"age\": 35,\n    \"city\": \"London\"\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.3 \u63a7\u5236\u952e\u7684\u6392\u5e8f<\/strong><\/h4>\n\n\n\n<p><code>json.dumps()<\/code> \u63d0\u4f9b\u4e86 <code>sort_keys<\/code> \u53c2\u6570\uff0c\u7528\u4e8e\u63a7\u5236\u8f93\u51fa\u7684 JSON \u952e\u7684\u6392\u5e8f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ndata = {\"name\": \"Charlie\", \"age\": 35, \"city\": \"London\"}\n\n# \u6309\u7167\u952e\u6392\u5e8f\u8f93\u51fa\njson_string = json.dumps(data, indent=4, sort_keys=True)\n\nprint(json_string)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"age\": 35,\n    \"city\": \"London\",\n    \"name\": \"Charlie\"\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.4 \u7f16\u7801 Python \u5bf9\u8c61\u4e3a JSON \u6587\u4ef6<\/strong><\/h4>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u5c06 Python \u5bf9\u8c61\u76f4\u63a5\u5199\u5165 JSON \u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\ndata = {\"name\": \"Diana\", \"age\": 40, \"city\": \"Berlin\"}\n\n# \u5c06 Python \u5bf9\u8c61\u5199\u5165 JSON \u6587\u4ef6\nwith open('output.json', 'w') as file:\n    json.dump(data, file, indent=4)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. JSON \u4e0e\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p>\u5f53 JSON \u6570\u636e\u4e2d\u5305\u542b\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65f6\uff0cPython \u9ed8\u8ba4\u65e0\u6cd5\u76f4\u63a5\u5904\u7406\u8fd9\u4e9b\u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>default<\/code> \u53c2\u6570\u6765\u6307\u5b9a\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>5.1 \u81ea\u5b9a\u4e49\u5bf9\u8c61\u7f16\u7801<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nclass Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def to_dict(self):\n        return {\"name\": self.name, \"age\": self.age}\n\n# \u521b\u5efa\u81ea\u5b9a\u4e49\u5bf9\u8c61\nperson = Person(\"Eva\", 28)\n\n# \u7f16\u7801\u65f6\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 `to_dict` \u65b9\u6cd5\njson_string = json.dumps(person, default=lambda p: p.to_dict(), indent=4)\n\nprint(json_string)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"Eva\",\n    \"age\": 28\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>5.2 \u81ea\u5b9a\u4e49\u5bf9\u8c61\u89e3\u7801<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nclass Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    @staticmethod\n    def from_dict(data):\n        return Person(data&#91;\"name\"], data&#91;\"age\"])\n\n# JSON \u5b57\u7b26\u4e32\njson_data = '{\"name\": \"Eva\", \"age\": 28}'\n\n# \u89e3\u7801 JSON \u5b57\u7b26\u4e32\u5e76\u521b\u5efa\u81ea\u5b9a\u4e49\u5bf9\u8c61\nperson = json.loads(json_data, object_hook=Person.from_dict)\n\nprint(person.name, person.age)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Eva 28<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>json.loads()<\/code><\/strong>: \u7528\u4e8e\u89e3\u6790 JSON \u5b57\u7b26\u4e32\u4e3a Python \u5bf9\u8c61\uff08\u5982\u5b57\u5178\u3001\u5217\u8868\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong><code>json.load()<\/code><\/strong>: \u7528\u4e8e\u89e3\u6790 JSON \u6587\u4ef6\u5185\u5bb9\u4e3a Python \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong><code>json.dumps()<\/code><\/strong>: \u7528\u4e8e\u5c06 Python \u5bf9\u8c61\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><strong><code>json.dump()<\/code><\/strong>: \u7528\u4e8e\u5c06 Python \u5bf9\u8c61\u5199\u5165 JSON \u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\uff1a\u4f7f\u7528 <code>indent<\/code> \u53c2\u6570\u4f7f\u8f93\u51fa\u7684 JSON \u6570\u636e\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u4f7f\u7528 <code>sort_keys<\/code> \u6392\u5e8f\u952e\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/strong>\uff1a\u901a\u8fc7 <code>default<\/code> \u53c2\u6570\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u7f16\u7801\uff0c\u4f7f\u7528 <code>object_hook<\/code> \u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u89e3\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python3 \u4e2d\uff0c\u5904\u7406 JSON \u6570\u636e\u975e\u5e38\u7b80\u5355\uff0c\u6807\u51c6\u5e93 json \u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684 API \u6765\u89e3\u6790 JSON [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2882,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2881","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2881","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=2881"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2881\/revisions"}],"predecessor-version":[{"id":2883,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2881\/revisions\/2883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2882"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}