{"id":1073209,"date":"2025-01-08T11:23:18","date_gmt":"2025-01-08T03:23:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073209.html"},"modified":"2025-01-08T11:23:20","modified_gmt":"2025-01-08T03:23:20","slug":"json%e6%96%87%e4%bb%b6%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e6%88%90python%e5%ad%97%e5%85%b8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073209.html","title":{"rendered":"json\u6587\u4ef6\u5982\u4f55\u8f6c\u6362\u6210python\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103201\/c097d8f1-0374-47b1-919d-baf94029d4b0.webp\" alt=\"json\u6587\u4ef6\u5982\u4f55\u8f6c\u6362\u6210python\u5b57\u5178\" \/><\/p>\n<p><p> <strong>json\u6587\u4ef6\u8f6c\u6362\u6210python\u5b57\u5178\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528Python\u7684json\u6a21\u5757\u3001\u4f7f\u7528json.load()\u51fd\u6570\u3001\u5904\u7406\u6587\u4ef6\u5f02\u5e38\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528json.load()\u51fd\u6570<\/strong>\u662f\u6700\u4e3a\u5173\u952e\u7684\u4e00\u70b9\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u76f4\u63a5\u5c06json\u6587\u4ef6\u7684\u5185\u5bb9\u52a0\u8f7d\u4e3aPython\u7684\u5b57\u5178\u683c\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u8fd9\u4e00\u70b9\uff0c\u5e76\u4ecb\u7ecd\u5176\u4ed6\u76f8\u5173\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528json.load()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>json.load()\u51fd\u6570\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u4ece\u6587\u4ef6\u5bf9\u8c61\u4e2d\u8bfb\u53d6JSON\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684Python\u6570\u636e\u7ed3\u6784\uff0c\u901a\u5e38\u662f\u5b57\u5178\u6216\u5217\u8868\u3002\u5177\u4f53\u4f7f\u7528\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165json\u6a21\u5757\uff0c\u8fd9\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406JSON\u6570\u636e\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528open()\u51fd\u6570\u6253\u5f00\u4e00\u4e2aJSON\u6587\u4ef6\uff0c\u5e76\u5c06\u8be5\u6587\u4ef6\u5bf9\u8c61\u4f20\u9012\u7ed9json.load()\u51fd\u6570\u3002<\/li>\n<li>json.load()\u51fd\u6570\u4f1a\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542bJSON\u6570\u636e\u7684Python\u5b57\u5178\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6253\u5f00\u5e76\u8bfb\u53d6json\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>data.json<\/code>\u662f\u5b58\u50a8JSON\u6570\u636e\u7684\u6587\u4ef6\uff0c<code>json.load(file)<\/code>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5b57\u5178\uff0c\u6700\u7ec8\u5b58\u50a8\u5728\u53d8\u91cf<code>data<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165json\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884cJSON\u6587\u4ef6\u548cPython\u5b57\u5178\u8f6c\u6362\u4e4b\u524d\uff0c\u5fc5\u987b\u5bfc\u5165json\u6a21\u5757\u3002json\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u76f4\u63a5\u4f7f\u7528\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6253\u5f00\u5e76\u8bfb\u53d6JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u7684open()\u51fd\u6570\u6253\u5f00JSON\u6587\u4ef6\u3002open()\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff08\u4f8b\u5982\uff0c&#39;r&#39;\u8868\u793a\u8bfb\u53d6\u6a21\u5f0f\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>data.json<\/code>\u662f\u6587\u4ef6\u540d\uff0c<code>&#39;r&#39;<\/code>\u8868\u793a\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002<code>with open(...) as file:<\/code>\u8bed\u6cd5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u6587\u4ef6\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6587\u4ef6\u5185\u5bb9\u4e0d\u7b26\u5408JSON\u683c\u5f0f\u3002\u4e3a\u4e86\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\uff0c\u9700\u8981\u4f7f\u7528try-except\u5757\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>try:<\/p>\n<p>    with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>        data = json.load(file)<\/p>\n<p>    print(data)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u540d\u6216\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002&quot;)<\/p>\n<p>except json.JSONDecodeError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u5185\u5bb9\u4e0d\u662f\u6709\u6548\u7684JSON\u683c\u5f0f\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6587\u4ef6\u672a\u627e\u5230\uff0c\u4f1a\u6355\u83b7FileNotFoundError\u5f02\u5e38\uff0c\u5e76\u6253\u5370\u76f8\u5e94\u7684\u9519\u8bef\u6d88\u606f\u3002\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u4e0d\u662f\u6709\u6548\u7684JSON\u683c\u5f0f\uff0c\u4f1a\u6355\u83b7json.JSONDecodeError\u5f02\u5e38\uff0c\u5e76\u6253\u5370\u76f8\u5e94\u7684\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528json.dumps()\u548cjson.loads()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\uff0cjson\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86json.dumps()\u548cjson.loads()\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u548cPython\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>json.dumps()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>json.dumps()\u51fd\u6570\u5c06Python\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u63a5\u53d7\u4e00\u4e2aPython\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>json_string = json.dumps(data)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178<code>data<\/code>\u88ab\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u5e76\u5b58\u50a8\u5728<code>json_string<\/code>\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>json.loads()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>json.loads()\u51fd\u6570\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u6570\u636e\u7ed3\u6784\u3002\u5b83\u63a5\u53d7\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<p>data = json.loads(json_string)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cJSON\u5b57\u7b26\u4e32<code>json_string<\/code>\u88ab\u8f6c\u6362\u4e3aPython\u5b57\u5178\uff0c\u5e76\u5b58\u50a8\u5728<code>data<\/code>\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5d4c\u5957JSON\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u6570\u636e\u901a\u5e38\u662f\u5d4c\u5957\u7684\uff0c\u5373\u5305\u542b\u5d4c\u5957\u7684\u5b57\u5178\u6216\u5217\u8868\u3002json\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u8fd9\u79cd\u5d4c\u5957\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;street&quot;: &quot;123 M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n St&quot;,<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>        &quot;zipcode&quot;: &quot;10001&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;phone_numbers&quot;: [&quot;123-456-7890&quot;, &quot;987-654-3210&quot;]<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>data = json.loads(json_string)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>address<\/code>\u5b57\u6bb5\u662f\u4e00\u4e2a\u5d4c\u5957\u7684\u5b57\u5178\uff0c<code>phone_numbers<\/code>\u5b57\u6bb5\u662f\u4e00\u4e2a\u5d4c\u5957\u7684\u5217\u8868\u3002json.loads()\u51fd\u6570\u80fd\u591f\u6b63\u786e\u89e3\u6790\u8fd9\u4e9b\u5d4c\u5957\u7ed3\u6784\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684Python\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5c06Python\u5b57\u5178\u5199\u5165JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4eceJSON\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff0c\u6709\u65f6\u8fd8\u9700\u8981\u5c06Python\u5b57\u5178\u5199\u5165JSON\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528json.dump()\u51fd\u6570\u5b8c\u6210\u8fd9\u4e00\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;street&quot;: &quot;123 Main St&quot;,<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>        &quot;zipcode&quot;: &quot;10001&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;phone_numbers&quot;: [&quot;123-456-7890&quot;, &quot;987-654-3210&quot;]<\/p>\n<p>}<\/p>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cPython\u5b57\u5178<code>data<\/code>\u88ab\u5199\u5165\u540d\u4e3a<code>output.json<\/code>\u7684\u6587\u4ef6\u4e2d\u3002json.dump()\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u5199\u5165\u7684Python\u5bf9\u8c61\u548c\u6587\u4ef6\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>json.dump()\u548cjson.dumps()\u51fd\u6570\u63d0\u4f9b\u4e86\u591a\u4e2a\u9009\u9879\uff0c\u7528\u4e8e\u683c\u5f0f\u5316\u8f93\u51faJSON\u6570\u636e\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u6dfb\u52a0\u7f29\u8fdb\uff0c\u4f7f\u7528<code>separators<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;street&quot;: &quot;123 Main St&quot;,<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>        &quot;zipcode&quot;: &quot;10001&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;phone_numbers&quot;: [&quot;123-456-7890&quot;, &quot;987-654-3210&quot;]<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(data, indent=4, separators=(&#39;,&#39;, &#39;: &#39;))<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>indent=4<\/code>\u53c2\u6570\u6307\u5b9a\u6bcf\u4e2a\u5c42\u7ea7\u7f29\u8fdb4\u4e2a\u7a7a\u683c\uff0c<code>separators=(&#39;,&#39;, &#39;: &#39;)<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u4e86\u952e\u503c\u5bf9\u548c\u9879\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4eceURL\u8bfb\u53d6JSON\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cJSON\u6570\u636e\u53ef\u80fd\u6765\u81ea\u7f51\u7edc\u8d44\u6e90\uff08\u4f8b\u5982\uff0cAPI\u63a5\u53e3\uff09\u3002\u53ef\u4ee5\u4f7f\u7528requests\u6a21\u5757\u4eceURL\u8bfb\u53d6JSON\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5requests\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u4eceURL\u8bfb\u53d6JSON\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>import requests<\/p>\n<p>url = &#39;https:\/\/api.example.com\/data&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>data = response.json()<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>requests.get(url)<\/code>\u53d1\u9001HTTP GET\u8bf7\u6c42\u5230\u6307\u5b9aURL\uff0c\u5e76\u8fd4\u56de\u54cd\u5e94\u5bf9\u8c61\u3002<code>response.json()<\/code>\u65b9\u6cd5\u89e3\u6790\u54cd\u5e94\u4e2d\u7684JSON\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>JSON\u6807\u51c6\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u6570\u7ec4\uff08\u5217\u8868\uff09\u548c\u5bf9\u8c61\uff08\u5b57\u5178\uff09\u3002\u4f46\u662f\uff0c\u6709\u65f6\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u65e5\u671f\u548c\u65f6\u95f4\u3002\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49JSON\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u6765\u5904\u7406\u8fd9\u4e9b\u590d\u6742\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\uff0c\u5c06datetime\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>from datetime import datetime<\/p>\n<p>class DateTimeEncoder(json.JSONEncoder):<\/p>\n<p>    def default(self, obj):<\/p>\n<p>        if isinstance(obj, datetime):<\/p>\n<p>            return obj.isoformat()<\/p>\n<p>        return super().default(obj)<\/p>\n<p>data = {<\/p>\n<p>    &#39;name&#39;: &#39;John&#39;,<\/p>\n<p>    &#39;timestamp&#39;: datetime.now()<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(data, cls=DateTimeEncoder)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7f16\u7801\u5668DateTimeEncoder\u7ee7\u627f\u81eajson.JSONEncoder\uff0c\u5e76\u91cd\u5199\u4e86default()\u65b9\u6cd5\uff0c\u5c06datetime\u5bf9\u8c61\u8f6c\u6362\u4e3aISO\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u76f8\u5e94\u5730\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u89e3\u7801\u5668\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>from datetime import datetime<\/p>\n<p>def datetime_parser(dct):<\/p>\n<p>    for key, value in dct.items():<\/p>\n<p>        try:<\/p>\n<p>            dct[key] = datetime.fromisoformat(value)<\/p>\n<p>        except (ValueError, TypeError):<\/p>\n<p>            continue<\/p>\n<p>    return dct<\/p>\n<p>json_string = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;timestamp&quot;: &quot;2023-10-01T12:34:56&quot;}&#39;<\/p>\n<p>data = json.loads(json_string, object_hook=datetime_parser)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u81ea\u5b9a\u4e49\u89e3\u7801\u5668datetime_parser()\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\uff0c\u5c1d\u8bd5\u5c06\u503c\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u5982\u679c\u8f6c\u6362\u5931\u8d25\uff0c\u5219\u4fdd\u7559\u539f\u503c\u3002\u4f7f\u7528object_hook\u53c2\u6570\u5c06\u81ea\u5b9a\u4e49\u89e3\u7801\u5668\u4f20\u9012\u7ed9json.loads()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06JSON\u6587\u4ef6\u8f6c\u6362\u4e3aPython\u5b57\u5178\uff0c\u5e76\u5904\u7406\u5404\u79cd\u590d\u6742\u6570\u636e\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u603b\u7ed3\u7684\u4e00\u4e9b\u5173\u952e\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528json.load()\u51fd\u6570\u8bfb\u53d6JSON\u6587\u4ef6<\/strong>\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002<\/li>\n<li>\u5904\u7406\u6587\u4ef6\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5065\u58ee\u6027\u3002<\/li>\n<li>\u4f7f\u7528json.dumps()\u548cjson.loads()\u51fd\u6570\u5728\u5b57\u7b26\u4e32\u548cPython\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u8f6c\u6362\u3002<\/li>\n<li>\u5904\u7406\u5d4c\u5957JSON\u6570\u636e\uff0c\u786e\u4fdd\u6b63\u786e\u89e3\u6790\u590d\u6742\u7ed3\u6784\u3002<\/li>\n<li>\u4f7f\u7528json.dump()\u51fd\u6570\u5c06Python\u5b57\u5178\u5199\u5165JSON\u6587\u4ef6\u3002<\/li>\n<li><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\uff0c\u63d0\u9ad8JSON\u6570\u636e\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n<li>\u4eceURL\u8bfb\u53d6JSON\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002<\/li>\n<li><strong>\u5904\u7406\u590d\u6742\u6570\u636e\u7c7b\u578b<\/strong>\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u5904\u7406\u975e\u6807\u51c6\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<p><p>\u8fd9\u4e9b\u6b65\u9aa4\u548c\u6280\u5de7\u5c06\u5e2e\u52a9\u4f60\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06JSON\u6587\u4ef6\u52a0\u8f7d\u5230Python\u4e2d\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u52a0\u8f7dJSON\u6587\u4ef6\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u89e3\u6790\u4e3a\u5b57\u5178\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\nwith open(&#39;file.json&#39;) as f:\n    data = json.load(f)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>data<\/code>\u5c31\u4f1a\u662f\u4e00\u4e2aPython\u5b57\u5178\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u65f6\u9047\u5230\u9519\u8bef\uff0c\u5982\u4f55\u8fdb\u884c\u8c03\u8bd5\uff1f<\/strong><br \/>\u5982\u679c\u5728\u5c06JSON\u6587\u4ef6\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\uff0c\u5e38\u89c1\u7684\u539f\u56e0\u53ef\u80fd\u662f\u6587\u4ef6\u683c\u5f0f\u4e0d\u6b63\u786e\u6216\u5305\u542b\u65e0\u6548\u7684JSON\u5b57\u7b26\u3002\u53ef\u4ee5\u4f7f\u7528<code>json.JSONDecodeError<\/code>\u5f02\u5e38\u6765\u6355\u6349\u8fd9\u4e9b\u9519\u8bef\u3002\u901a\u8fc7\u6355\u6349\u5f02\u5e38\uff0c\u53ef\u4ee5\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5e76\u5b9a\u4f4d\u95ee\u9898\u6240\u5728\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\ntry:\n    with open(&#39;file.json&#39;) as f:\n        data = json.load(f)\nexcept json.JSONDecodeError as e:\n    print(f&quot;JSON\u89e3\u6790\u9519\u8bef: {e}&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06Python\u5b57\u5178\u4fdd\u5b58\u4e3aJSON\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5c06Python\u5b57\u5178\u4fdd\u5b58\u4e3aJSON\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dump()<\/code>\u65b9\u6cd5\u3002\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u5c06\u5b57\u5178\u5199\u5165\u8be5\u6587\u4ef6\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\ndata = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}\nwith open(&#39;output.json&#39;, &#39;w&#39;) as f:\n    json.dump(data, f)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>output.json<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u5178\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"json\u6587\u4ef6\u8f6c\u6362\u6210python\u5b57\u5178\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528Python\u7684json\u6a21\u5757\u3001\u4f7f\u7528json.load()\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1073215,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073209"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1073209"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073209\/revisions"}],"predecessor-version":[{"id":1073217,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073209\/revisions\/1073217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073215"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}