{"id":1078032,"date":"2025-01-08T12:07:48","date_gmt":"2025-01-08T04:07:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078032.html"},"modified":"2025-01-08T12:07:51","modified_gmt":"2025-01-08T04:07:51","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2json%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1078032.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362json\u5b57\u7b26\u4e32\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181804\/23483c02-9149-4dee-8826-884fbab9080a.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362json\u5b57\u7b26\u4e32\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u8f6c\u6362JSON\u5b57\u7b26\u4e32\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528json\u6a21\u5757\u4e2d\u7684loads\u548cdumps\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165json\u6a21\u5757\u3001\u4f7f\u7528json.loads()\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3001\u4f7f\u7528json.dumps()\u65b9\u6cd5\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3001\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u6765\u7f8e\u5316\u8f93\u51fa\u3002<\/strong> \u4f8b\u5982\uff0c\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u53ef\u4ee5\u4f7f\u8f93\u51fa\u66f4\u52a0\u6613\u8bfb\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u8fd9\u4e9b\u8f6c\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165json\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5904\u7406JSON\u6570\u636e\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5bfc\u5165Python\u7684json\u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406JSON\u6570\u636e\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u5de5\u5177\u3002\u901a\u8fc7\u5bfc\u5165json\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5176\u4e2d\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0JSON\u5b57\u7b26\u4e32\u548cPython\u5bf9\u8c61\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u4f7f\u7528json.loads()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u8fd9\u662f\u5904\u7406\u548c\u64cd\u4f5cJSON\u6570\u636e\u7684\u7b2c\u4e00\u6b65\u3002json.loads()\u65b9\u6cd5\u4f1a\u89e3\u6790JSON\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u76f8\u5e94\u7684Python\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u5b57\u5178\u6216\u5217\u8868\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_string = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<p>python_obj = json.loads(json_string)<\/p>\n<p>print(python_obj)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u4f7f\u7528json.dumps()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2aPython\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002\u8fd9\u5bf9\u4e8e\u5c06Python\u6570\u636e\u5e8f\u5217\u5316\u4e3aJSON\u683c\u5f0f\u4ee5\u4fbf\u8fdb\u884c\u5b58\u50a8\u6216\u4f20\u8f93\u975e\u5e38\u6709\u7528\u3002json.dumps()\u65b9\u6cd5\u4f1a\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">python_obj = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>json_string = json.dumps(python_obj)<\/p>\n<p>print(json_string)  # \u8f93\u51fa: {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7f8e\u5316JSON\u5b57\u7b26\u4e32\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u8f6c\u6362Python\u5bf9\u8c61\u4e3aJSON\u5b57\u7b26\u4e32\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528json.dumps()\u65b9\u6cd5\u7684\u53c2\u6570\u6765\u7f8e\u5316\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u53ef\u4ee5\u4f7f\u8f93\u51fa\u7684JSON\u5b57\u7b26\u4e32\u5177\u6709\u826f\u597d\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_string = json.dumps(python_obj, indent=4)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\u683c\u5f0f\u5316\u7684JSON\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u590d\u6742\u7684JSON\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u66f4\u590d\u6742\u7684JSON\u6570\u636e\u7ed3\u6784\uff0c\u5305\u62ec\u5d4c\u5957\u7684\u5b57\u5178\u548c\u5217\u8868\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u4f7f\u7528json.loads()\u548cjson.dumps()\u65b9\u6cd5\u6765\u5904\u7406\u548c\u8f6c\u6362\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">complex_json_string = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;name&quot;: &quot;Alice&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;: [<\/p>\n<p>        {&quot;type&quot;: &quot;home&quot;, &quot;number&quot;: &quot;212-555-1234&quot;},<\/p>\n<p>        {&quot;type&quot;: &quot;work&quot;, &quot;number&quot;: &quot;646-555-5678&quot;}<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>python_obj = json.loads(complex_json_string)<\/p>\n<p>print(json.dumps(python_obj, indent=4))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>    &quot;name&quot;: &quot;Alice&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;: [<\/p>\n<p>        {<\/p>\n<p>            &quot;type&quot;: &quot;home&quot;,<\/p>\n<p>            &quot;number&quot;: &quot;212-555-1234&quot;<\/p>\n<p>        },<\/p>\n<p>        {<\/p>\n<p>            &quot;type&quot;: &quot;work&quot;,<\/p>\n<p>            &quot;number&quot;: &quot;646-555-5678&quot;<\/p>\n<p>        }<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406JSON\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5904\u7406JSON\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u6765\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6\u3002\u4f7f\u7528json.load()\u65b9\u6cd5\u53ef\u4ee5\u5c06JSON\u6587\u4ef6\u7684\u5185\u5bb9\u8bfb\u53d6\u4e3aPython\u5bf9\u8c61\uff0c\u4f7f\u7528json.dump()\u65b9\u6cd5\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u5199\u5165JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8bfb\u53d6JSON\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    python_obj = json.load(file)<\/p>\n<p>    print(python_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5199\u5165JSON\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(python_obj, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u7279\u6b8a\u7684\u6570\u636e\u7c7b\u578b<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9bPython\u4e0d\u76f4\u63a5\u652f\u6301\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316\u65b9\u6cd5<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>class DateTimeEncoder(json.JSONEncoder):<\/p>\n<p>    def default(self, obj):<\/p>\n<p>        if isinstance(obj, datetime.datetime):<\/p>\n<p>            return obj.isoformat()<\/p>\n<p>        return super().default(obj)<\/p>\n<p>now = datetime.datetime.now()<\/p>\n<p>json_string = json.dumps({&quot;current_time&quot;: now}, cls=DateTimeEncoder)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u81ea\u5b9a\u4e49\u53cd\u5e8f\u5217\u5316\u65b9\u6cd5<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def datetime_parser(dct):<\/p>\n<p>    for k, v in dct.items():<\/p>\n<p>        try:<\/p>\n<p>            dct[k] = datetime.datetime.fromisoformat(v)<\/p>\n<p>        except (ValueError, TypeError):<\/p>\n<p>            pass<\/p>\n<p>    return dct<\/p>\n<p>json_string = &#39;{&quot;current_time&quot;: &quot;2023-11-25T12:34:56.789012&quot;}&#39;<\/p>\n<p>python_obj = json.loads(json_string, object_hook=datetime_parser)<\/p>\n<p>print(python_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u548c\u590d\u6742\u7a0b\u5ea6\u7684JSON\u6570\u636e\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5d4c\u5957\u6570\u636e\u5904\u7406\uff0cPython\u7684json\u6a21\u5757\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u6765\u8f6c\u6362JSON\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u4e2d\u7684<code>loads()<\/code>\u51fd\u6570\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5bfc\u5165<code>json<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>json.loads()<\/code>\u5e76\u4f20\u5165\u4f60\u7684JSON\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}&#39;\ndata = json.loads(json_string)\nprint(data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u6210\u529f\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u4f7f\u7528<code>json.dumps()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06Python\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>json<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f20\u5165\u4f60\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\ndata = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}\njson_string = json.dumps(data)\nprint(json_string)  # \u8f93\u51fa: {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}\n<\/code><\/pre>\n<p>\u8fd9\u4f7f\u5f97\u5c06\u6570\u636e\u4ee5JSON\u683c\u5f0f\u5b58\u50a8\u6216\u4f20\u8f93\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406JSON\u5b57\u7b26\u4e32\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8f6c\u6362JSON\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u9519\u8bef\u6216\u4e0d\u7b26\u5408JSON\u89c4\u8303\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30&#39;  # \u7f3a\u5c11\u95ed\u5408\u5927\u62ec\u53f7\ntry:\n    data = json.loads(json_string)\nexcept json.JSONDecodeError as e:\n    print(f&quot;\u89e3\u6790\u9519\u8bef: {e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bc6\u522b\u5e76\u5904\u7406\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f6c\u6362JSON\u5b57\u7b26\u4e32\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528json\u6a21\u5757\u4e2d\u7684loads\u548cdumps\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a [&hellip;]","protected":false},"author":3,"featured_media":1078044,"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\/1078032"}],"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=1078032"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078032\/revisions"}],"predecessor-version":[{"id":1078047,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078032\/revisions\/1078047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078044"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}