{"id":940458,"date":"2024-12-26T21:00:33","date_gmt":"2024-12-26T13:00:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940458.html"},"modified":"2024-12-26T21:00:35","modified_gmt":"2024-12-26T13:00:35","slug":"python%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85josn","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940458.html","title":{"rendered":"python\u5982\u4f55\u5b89\u88c5josn"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075059\/023915e7-5ada-4d15-b8e9-aed99ae3d459.webp\" alt=\"python\u5982\u4f55\u5b89\u88c5josn\" \/><\/p>\n<p><p> <strong>Python\u4e0d\u9700\u8981\u5b89\u88c5JSON\u6a21\u5757\u3001\u56e0\u4e3aJSON\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3001\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u81ea\u5e26\u7684\u6807\u51c6\u5e93\u4e2d\u5df2\u7ecf\u5305\u542b\u4e86JSON\u6a21\u5757\uff0c\u56e0\u6b64\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u60a8\u65e0\u9700\u8fdb\u884c\u989d\u5916\u7684\u5b89\u88c5\u4fbf\u53ef\u4ee5\u4f7f\u7528\u3002JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0cPython\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528JSON\u6a21\u5757\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684JSON\u6a21\u5757<\/strong><\/h2>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u81ea\u5e26\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684<code>json<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728Python\u4e2d\u5904\u7406JSON\u6570\u636e\u3002\u8fd9\u610f\u5473\u7740\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u8fdb\u884c\u4efb\u4f55\u989d\u5916\u7684\u5b89\u88c5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>1.1\u3001\u52a0\u8f7dJSON\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5148\u5bfc\u5165\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bfc\u5165\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u63d0\u4f9b\u7684\u5404\u79cd\u529f\u80fd\u6765\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>1.2\u3001\u89e3\u6790JSON\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u89e3\u6790JSON\u6570\u636e\u662f\u5c06JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>loads()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\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_str = &#39;{&quot;name&quot;: &quot;John&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>python_obj = json.loads(json_str)<\/p>\n<p>print(python_obj)<\/p>\n<p>print(type(python_obj))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cJSON\u5b57\u7b26\u4e32\u88ab\u89e3\u6790\u4e3aPython\u7684\u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>1.3\u3001\u751f\u6210JSON\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u751f\u6210JSON\u6570\u636e\u662f\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u7684<code>dumps()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>Python\u5b57\u5178<\/strong><\/h2>\n<p>python_obj = {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u751f\u6210JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_str = json.dumps(python_obj)<\/p>\n<p>print(json_str)<\/p>\n<p>print(type(json_str))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0cPython\u5b57\u5178\u88ab\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u5904\u7406JSON\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u6570\u636e\u901a\u5e38\u4fdd\u5b58\u5728\u6587\u4ef6\u4e2d\u3002Python\u7684<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>2.1\u3001\u8bfb\u53d6JSON\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u7684<code>load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\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>    data = json.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>data.json<\/code>\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u88ab\u8bfb\u53d6\u5e76\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>2.2\u3001\u5199\u5165JSON\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u7684<code>dump()<\/code>\u65b9\u6cd5\u5c06Python\u5bf9\u8c61\u5199\u5165\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>Python\u5b57\u5178<\/strong><\/h2>\n<p>data = {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u5199\u5165JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0cPython\u5b57\u5178\u88ab\u8f6c\u6362\u4e3aJSON\u6570\u636e\u5e76\u5199\u5165\u5230<code>data.json<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e09\u3001JSON\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n<p><p><code>json<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u6bd4\u5982\u81ea\u5b9a\u4e49\u7f16\u7801\u548c\u89e3\u7801\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u7b49\u3002<\/p>\n<\/p>\n<p><h2>3.1\u3001\u81ea\u5b9a\u4e49\u7f16\u7801\u548c\u89e3\u7801<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49JSON\u7f16\u7801\u548c\u89e3\u7801\u8fc7\u7a0b\u3002<code>json<\/code>\u6a21\u5757\u5141\u8bb8\u60a8\u901a\u8fc7\u5b50\u7c7b\u5316<code>JSONEncoder<\/code>\u548c<code>JSONDecoder<\/code>\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>class CustomEncoder(json.JSONEncoder):<\/p>\n<p>    def default(self, obj):<\/p>\n<p>        # \u81ea\u5b9a\u4e49\u7f16\u7801\u8fc7\u7a0b<\/p>\n<p>        if isinstance(obj, complex):<\/p>\n<p>            return {&quot;real&quot;: obj.real, &quot;imag&quot;: obj.imag}<\/p>\n<p>        return super().default(obj)<\/p>\n<p>data = {&quot;complex_number&quot;: 3 + 4j}<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u7f16\u7801\u5668<\/strong><\/h2>\n<p>json_str = json.dumps(data, cls=CustomEncoder)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u6765\u5904\u7406\u590d\u6742\u6570\u7684\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h2>3.2\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/h2>\n<\/p>\n<p><p><code>json.dumps()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u8bb8\u591a\u9009\u9879\u6765\u63a7\u5236\u8f93\u51fa\u7684\u683c\u5f0f\uff0c\u6bd4\u5982<code>indent<\/code>\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u683c\u5f0f\u5316\u7684JSON\u5b57\u7b26\u4e32\uff0c\u4fbf\u4e8e\u9605\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/h2>\n<p>json_str = json.dumps(data, indent=4)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>indent<\/code>\u53c2\u6570\uff0c\u751f\u6210\u7684JSON\u5b57\u7b26\u4e32\u5c06\u4ee5\u7f29\u8fdb\u683c\u5f0f\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h2>3.3\u3001\u5904\u7406\u6d6e\u70b9\u6570\u7cbe\u5ea6<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6d6e\u70b9\u6570\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u63a7\u5236\u5176\u7cbe\u5ea6\u3002<code>json<\/code>\u6a21\u5757\u5141\u8bb8\u60a8\u901a\u8fc7<code>dumps()<\/code>\u65b9\u6cd5\u7684<code>float_precision<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&quot;value&quot;: 123.456789}<\/p>\n<h2><strong>\u63a7\u5236\u6d6e\u70b9\u6570\u7cbe\u5ea6<\/strong><\/h2>\n<p>json_str = json.dumps(data, indent=4, default=str)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u6d6e\u70b9\u6570\u7684\u663e\u793a\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u56db\u3001JSON\u4e0e\u5176\u4ed6\u6570\u636e\u683c\u5f0f\u7684\u4e92\u64cd\u4f5c<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5c06JSON\u6570\u636e\u4e0e\u5176\u4ed6\u6570\u636e\u683c\u5f0f\uff08\u5982XML\u3001CSV\u7b49\uff09\u76f8\u4e92\u8f6c\u6362\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u6765\u652f\u6301\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>4.1\u3001JSON\u4e0eXML\u7684\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>XML\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u683c\u5f0f\uff0cPython\u4e2d\u6709\u8bb8\u591a\u5e93\uff08\u5982<code>xmltodict<\/code>\uff09\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728JSON\u548cXML\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>import xmltodict<\/p>\n<h2><strong>JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_str = &#39;{&quot;name&quot;: &quot;John&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>python_obj = json.loads(json_str)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aXML<\/strong><\/h2>\n<p>xml_str = xmltodict.unparse({&quot;root&quot;: python_obj})<\/p>\n<p>print(xml_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cJSON\u6570\u636e\u88ab\u8f6c\u6362\u4e3aXML\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>4.2\u3001JSON\u4e0eCSV\u7684\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>CSV\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\u7684\u5e38\u89c1\u683c\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u548c<code>json<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u4e24\u8005\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>import json<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u8f6c\u6362\u4e3aJSON<\/strong><\/h2>\n<p>csv_file = &#39;data.csv&#39;<\/p>\n<p>json_file = &#39;data.json&#39;<\/p>\n<p>with open(csv_file, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.DictReader(file)<\/p>\n<p>    rows = list(reader)<\/p>\n<p>with open(json_file, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(rows, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cCSV\u6587\u4ef6\u88ab\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h2>5.1\u3001\u5904\u7406\u9519\u8bef\u7684JSON\u683c\u5f0f<\/h2>\n<\/p>\n<p><p>\u89e3\u6790\u9519\u8bef\u7684JSON\u683c\u5f0f\u65f6\uff0c<code>json.loads()<\/code>\u65b9\u6cd5\u4f1a\u629b\u51fa<code>JSONDecodeError<\/code>\u5f02\u5e38\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_str = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;&#39;<\/p>\n<p>try:<\/p>\n<p>    data = json.loads(json_str)<\/p>\n<p>except json.JSONDecodeError as e:<\/p>\n<p>    print(f&quot;JSON\u89e3\u7801\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u4e86\u89e3\u7801\u9519\u8bef\u5e76\u8fdb\u884c\u4e86\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>5.2\u3001\u5904\u7406\u975e\u6807\u51c6JSON<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e0d\u5b8c\u5168\u7b26\u5408JSON\u6807\u51c6\u7684\u6570\u636e\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u89e3\u7801\u5668\u6216\u5728\u89e3\u6790\u524d\u9884\u5904\u7406\u6570\u636e\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_str = &quot;{&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}&quot;  # \u4f7f\u7528\u5355\u5f15\u53f7\u7684\u975e\u6807\u51c6JSON<\/p>\n<h2><strong>\u66ff\u6362\u5355\u5f15\u53f7\u4e3a\u53cc\u5f15\u53f7<\/strong><\/h2>\n<p>json_str = json_str.replace(&quot;&#39;&quot;, &#39;&quot;&#39;)<\/p>\n<p>data = json.loads(json_str)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9884\u5904\u7406\u6570\u636e\u4ee5\u7b26\u5408JSON\u6807\u51c6\u3002<\/p>\n<\/p>\n<p><h2>5.3\u3001\u907f\u514dUnicode\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>json.dumps()<\/code>\u4f1a\u5c06\u975eASCII\u5b57\u7b26\u7f16\u7801\u4e3aUnicode\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>ensure_ascii=False<\/code>\u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&quot;name&quot;: &quot;J\u00f6hn&quot;, &quot;city&quot;: &quot;M\u00fcnchen&quot;}<\/p>\n<h2><strong>\u907f\u514dUnicode\u7f16\u7801<\/strong><\/h2>\n<p>json_str = json.dumps(data, ensure_ascii=False)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u975eASCII\u5b57\u7b26\u5c06\u4fdd\u6301\u5176\u539f\u59cb\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<hr>\n<p><p>Python\u4e2d\u7684JSON\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u5728Python\u7a0b\u5e8f\u4e2d\u8f7b\u677e\u5730\u5904\u7406JSON\u6570\u636e\u3002\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1\u672c\u6587\u4e2d\u4ecb\u7ecd\u7684\u5404\u79cd\u6280\u672f\u548c\u65b9\u6cd5\uff0c\u60a8\u5c06\u80fd\u591f\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406JSON\u6570\u636e\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u5b9e\u9645\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5b89\u88c5JSON\u6a21\u5757\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u4e2d\u5df2\u7ecf\u5305\u542b\u4e86JSON\u6a21\u5757\uff0c\u56e0\u6b64\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u60a8\u53ef\u4ee5\u76f4\u63a5\u5728\u60a8\u7684Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u5b83\uff0c\u4f7f\u7528<code>import json<\/code>\u6765\u8bbf\u95eeJSON\u529f\u80fd\u3002\u8fd9\u4f7f\u5f97\u5904\u7406JSON\u6570\u636e\u53d8\u5f97\u5341\u5206\u7b80\u5355\u548c\u65b9\u4fbf\u3002<\/p>\n<p><strong>JSON\u6a21\u5757\u7684\u5e38\u7528\u529f\u80fd\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>JSON\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u529f\u80fd\u6765\u5904\u7406JSON\u6570\u636e\uff0c\u5305\u62ec<code>json.dumps()<\/code>\u548c<code>json.loads()<\/code>\u3002<code>json.dumps()<\/code>\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u800c<code>json.loads()<\/code>\u5219\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u6b64\u5916\uff0c\u8fd8\u6709<code>json.dump()<\/code>\u548c<code>json.load()<\/code>\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u4e2d\u7684JSON\u6570\u636e\uff0c\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97\u4e0eJSON\u683c\u5f0f\u7684\u6570\u636e\u4ea4\u4e92\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406JSON\u6587\u4ef6\uff1f<\/strong><br \/>\u5904\u7406JSON\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u5c06\u5176\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u76f8\u5bf9\u5730\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>json.dump()<\/code>\u5c06Python\u5bf9\u8c61\u5199\u5165JSON\u6587\u4ef6\u3002\u786e\u4fdd\u4f7f\u7528<code>with open()<\/code>\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u5173\u95ed\uff0c\u907f\u514d\u6f5c\u5728\u7684\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898\u3002\u8fd9\u6837\u7684\u64cd\u4f5c\u4f7f\u5f97\u6570\u636e\u7684\u5b58\u50a8\u548c\u8bfb\u53d6\u90fd\u53d8\u5f97\u9ad8\u6548\u800c\u5b89\u5168\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e0d\u9700\u8981\u5b89\u88c5JSON\u6a21\u5757\u3001\u56e0\u4e3aJSON\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3001\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002 Python\u81ea [&hellip;]","protected":false},"author":3,"featured_media":940462,"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\/940458"}],"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=940458"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940458\/revisions"}],"predecessor-version":[{"id":940463,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940458\/revisions\/940463"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/940462"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=940458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=940458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=940458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}