{"id":1009092,"date":"2024-12-27T11:08:38","date_gmt":"2024-12-27T03:08:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009092.html"},"modified":"2024-12-27T11:08:42","modified_gmt":"2024-12-27T03:08:42","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%a7%a3%e6%9e%90json","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1009092.html","title":{"rendered":"\u5982\u4f55\u7528python\u89e3\u6790json"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084134\/493d1e46-8025-4602-9ad1-11f99cf294ad.webp\" alt=\"\u5982\u4f55\u7528python\u89e3\u6790json\" \/><\/p>\n<p><p> \u5728\u4f7f\u7528Python\u89e3\u6790JSON\u65f6\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u5e93\u3001\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3001\u4f7f\u7528<code>json.load()<\/code>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u548c\u89e3\u6790JSON\u6570\u636e<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>json.loads()<\/code>\u548c<code>json.load()<\/code>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u89e3\u6790JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PYTHON JSON\u5e93\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>Python\u7684<code>json<\/code>\u5e93\u662f\u5185\u7f6e\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06JSON\u6570\u636e\u4e0ePython\u5bf9\u8c61\u76f8\u4e92\u8f6c\u6362\u3002JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u7c7b\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u4fbf\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002Python\u7684<code>json<\/code>\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u53c8\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5904\u7406JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001JSON\u5e93\u7684\u5e38\u7528\u529f\u80fd<\/h4>\n<\/p>\n<ul>\n<li><strong><code>json.loads()<\/code><\/strong>\uff1a\u5c06JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\uff08\u4f8b\u5982\u5b57\u5178\u6216\u5217\u8868\uff09\u3002<\/li>\n<li><strong><code>json.load()<\/code><\/strong>\uff1a\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002<\/li>\n<li><strong><code>json.dumps()<\/code><\/strong>\uff1a\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong><code>json.dump()<\/code><\/strong>\uff1a\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u89e3\u6790JSON\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>json.loads()<\/code><\/h4>\n<\/p>\n<p><p><code>json.loads()<\/code>\u7528\u4e8e\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_string = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<h2><strong>\u89e3\u6790JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>data = json.loads(json_string)<\/p>\n<p>print(data)<\/p>\n<p>print(data[&#39;name&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<strong>\u6211\u4eec\u4f7f\u7528<code>json.loads()<\/code>\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5b57\u5178<\/strong>\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u7684\u952e\u6765\u8bbf\u95ee\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u590d\u6742\u7684JSON\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>JSON\u6570\u636e\u53ef\u80fd\u5305\u542b\u5d4c\u5957\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u548c\u5b57\u5178\u7684\u5d4c\u5957\u3002<code>json.loads()<\/code>\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5d4c\u5957JSON\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5d4c\u5957\u7684JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_string = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>  &quot;name&quot;: &quot;Bob&quot;,<\/p>\n<p>  &quot;age&quot;: 25,<\/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>  },<\/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<h2><strong>\u89e3\u6790\u5d4c\u5957\u7684JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>data = json.loads(json_string)<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u6570\u636e<\/strong><\/h2>\n<p>print(data[&#39;address&#39;][&#39;city&#39;])<\/p>\n<p>print(data[&#39;phone_numbers&#39;][0])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u548c\u89e3\u6790JSON\u6570\u636e<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>json.load()<\/code><\/h4>\n<\/p>\n<p><p>\u5982\u679cJSON\u6570\u636e\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u548c\u89e3\u6790\u5b83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6253\u5f00JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    # \u89e3\u6790JSON\u6587\u4ef6<\/p>\n<p>    data = json.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u4e00\u4e2aJSON\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>json.load()<\/code>\u89e3\u6790\u6587\u4ef6\u5185\u5bb9\u3002<strong>\u8fd9\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u578bJSON\u6587\u4ef6<\/strong>\uff0c\u56e0\u4e3a\u53ef\u4ee5\u907f\u514d\u5c06\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\u8f7d\u5165\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u5d4c\u5957JSON<\/h4>\n<\/p>\n<p><p>\u4e0e\u89e3\u6790\u5b57\u7b26\u4e32\u7c7b\u4f3c\uff0c<code>json.load()<\/code>\u4e5f\u53ef\u4ee5\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u5d4c\u5957JSON\u7ed3\u6784\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe<code>data.json<\/code>\u7684\u5185\u5bb9\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;employees&quot;: [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 25}<\/p>\n<p>  ],<\/p>\n<p>  &quot;company&quot;: {<\/p>\n<p>    &quot;name&quot;: &quot;TechCorp&quot;,<\/p>\n<p>    &quot;location&quot;: &quot;Silicon Valley&quot;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u89e3\u6790\u5e76\u8bbf\u95ee\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u6570\u636e<\/strong><\/h2>\n<p>for employee in data[&#39;employees&#39;]:<\/p>\n<p>    print(employee[&#39;name&#39;])<\/p>\n<p>print(data[&#39;company&#39;][&#39;name&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>json.dumps()<\/code><\/h4>\n<\/p>\n<p><p><code>json.dumps()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u5728\u9700\u8981\u5c06\u6570\u636e\u53d1\u9001\u5230Web\u670d\u52a1\u6216\u4fdd\u5b58\u5230\u6587\u4ef6\u65f6\u975e\u5e38\u6709\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>Python\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Charlie&quot;,<\/p>\n<p>    &quot;age&quot;: 35,<\/p>\n<p>    &quot;city&quot;: &quot;San Francisco&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\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<strong><code>json.dumps()<\/code>\u5c06Python\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>json.dumps()<\/code>\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7f\u5176\u66f4\u52a0\u6613\u4e8e\u9605\u8bfb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Charlie&quot;,<\/p>\n<p>    &quot;age&quot;: 35,<\/p>\n<p>    &quot;city&quot;: &quot;San Francisco&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/h2>\n<p>json_string = json.dumps(data, indent=4)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5199\u5165JSON\u6570\u636e\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>json.dump()<\/code><\/h4>\n<\/p>\n<p><p><code>json.dump()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5e76\u5199\u5165\u6587\u4ef6\u3002\u8fd9\u5bf9\u4e8e\u5c06\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>Python\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Dana&quot;,<\/p>\n<p>    &quot;age&quot;: 28,<\/p>\n<p>    &quot;city&quot;: &quot;Los Angeles&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u5199\u5165JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<strong>\u6211\u4eec\u5c06Python\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5e76\u5199\u5165\u5230\u6587\u4ef6<code>output.json<\/code>\u4e2d<\/strong>\uff0c\u540c\u65f6\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406JSON\u6570\u636e\u4e2d\u7684\u5f02\u5e38<\/h3>\n<\/p>\n<p><h4>1\u3001\u6355\u83b7\u89e3\u6790\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u89e3\u6790JSON\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u4e0d\u6b63\u786e\u6216\u65e0\u6548\u7684JSON\u3002\u8fd9\u65f6\u9700\u8981\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9519\u8bef\u5904\u7406\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;{&quot;name&quot;: &quot;Eve&quot;, &quot;age&quot;: &quot;twenty&quot;}&#39;  # age\u5b57\u6bb5\u503c\u4e0d\u662f\u6709\u6548\u7684JSON\u683c\u5f0f<\/p>\n<p>try:<\/p>\n<p>    data = json.loads(json_string)<\/p>\n<p>except json.JSONDecodeError as e:<\/p>\n<p>    print(f&quot;JSON\u89e3\u6790\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6JSON\u6587\u4ef6\u65f6\uff0c\u4e5f\u9700\u8981\u5904\u7406\u53ef\u80fd\u7684IO\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>try:<\/p>\n<p>    with open(&#39;non_existent_file.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>        data = json.load(file)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u672a\u627e\u5230&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u6743\u9650\u9519\u8bef&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u7684<code>json<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u89e3\u6790\u548c\u5904\u7406JSON\u6570\u636e\u3002<strong>\u638c\u63e1<code>json.loads()<\/code>\u548c<code>json.load()<\/code>\u662f\u89e3\u6790JSON\u6570\u636e\u7684\u5173\u952e<\/strong>\uff0c\u800c<code>json.dumps()<\/code>\u548c<code>json.dump()<\/code>\u5219\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002\u901a\u8fc7\u5904\u7406JSON\u6570\u636e\u4e2d\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002\u6b64\u5916\uff0c\u7406\u89e3\u5982\u4f55\u5904\u7406\u590d\u6742\u7684\u5d4c\u5957JSON\u7ed3\u6784\u5bf9\u4e8e\u6709\u6548\u5229\u7528JSON\u6570\u636e\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5730\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6JSON\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bfb\u53d6JSON\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684JSON\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u6587\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\nwith open(&#39;your_file.json&#39;, &#39;r&#39;) as file:\n    data = json.load(file)\n    print(data)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u5c06JSON\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u52a0\u8f7d\u5230Python\u7684\u5b57\u5178\u6216\u5217\u8868\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684JSON\u89e3\u6790\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u6790JSON\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u9519\u8bef\u6216\u5176\u4ed6\u95ee\u9898\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\ntry:\n    with open(&#39;your_file.json&#39;, &#39;r&#39;) as file:\n        data = json.load(file)\nexcept json.JSONDecodeError as e:\n    print(f&quot;\u89e3\u6790\u9519\u8bef\uff1a{e}&quot;)\nexcept FileNotFoundError as e:\n    print(f&quot;\u6587\u4ef6\u672a\u627e\u5230\uff1a{e}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff1f<\/strong><br \/>\u8981\u5c06Python\u5bf9\u8c61\uff08\u5982\u5b57\u5178\u6216\u5217\u8868\uff09\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dumps()<\/code>\u6216<code>json.dump()<\/code>\u3002<code>dumps()<\/code>\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u800c<code>dump()<\/code>\u5219\u5c06\u5176\u5199\u5165\u6587\u4ef6\u3002\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}\n\n# \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\njson_string = json.dumps(data)\nprint(json_string)\n\n# \u5199\u5165\u6587\u4ef6\nwith open(&#39;output.json&#39;, &#39;w&#39;) as file:\n    json.dump(data, file)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06Python\u6570\u636e\u7ed3\u6784\u4fdd\u5b58\u4e3aJSON\u683c\u5f0f\uff0c\u65b9\u4fbf\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u4f7f\u7528Python\u89e3\u6790JSON\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684json\u5e93\u3001\u4f7f\u7528json.loads()\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c [&hellip;]","protected":false},"author":3,"featured_media":1009103,"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\/1009092"}],"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=1009092"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009092\/revisions"}],"predecessor-version":[{"id":1009104,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009092\/revisions\/1009104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009103"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}