{"id":971544,"date":"2024-12-27T05:35:56","date_gmt":"2024-12-26T21:35:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/971544.html"},"modified":"2024-12-27T05:35:57","modified_gmt":"2024-12-26T21:35:57","slug":"python%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90json","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/971544.html","title":{"rendered":"python\u5982\u4f55\u81ea\u52a8\u751f\u6210json"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194622\/ac0ae851-f984-4eea-8545-83ba9f773a1b.webp\" alt=\"python\u5982\u4f55\u81ea\u52a8\u751f\u6210json\" \/><\/p>\n<p><p> <strong>Python\u81ea\u52a8\u751f\u6210JSON\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u3001\u5229\u7528<code>dict<\/code>\u6570\u636e\u7ed3\u6784\u3001\u7ed3\u5408\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757<\/strong>\u662f\u6700\u4e3a\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u8f7b\u677e\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u81ea\u52a8\u751f\u6210JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e<code>json<\/code>\u6a21\u5757\u662f\u751f\u6210\u548c\u5904\u7406JSON\u6570\u636e\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\u3002\u5728\u8be5\u6a21\u5757\u4e2d\uff0c<code>dumps()<\/code>\u548c<code>dump()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u548c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>1.1\u3001\u4f7f\u7528<code>json.dumps()<\/code>\u751f\u6210JSON\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p><code>json.dumps()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06Python\u5bf9\u8c61\uff08\u5982\u5b57\u5178\u3001\u5217\u8868\u7b49\uff09\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f20\u5165\u5f85\u8f6c\u6362\u7684\u5bf9\u8c61\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &#39;name&#39;: &#39;John Doe&#39;,<\/p>\n<p>    &#39;age&#39;: 30,<\/p>\n<p>    &#39;city&#39;: &#39;New York&#39;<\/p>\n<p>}<\/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\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aPython\u5b57\u5178\u5bf9\u8c61<code>data<\/code>\uff0c\u901a\u8fc7<code>json.dumps()<\/code>\u5c06\u5176\u8f6c\u5316\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5e76\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>1.2\u3001\u4f7f\u7528<code>json.dump()<\/code>\u751f\u6210JSON\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>json.dump()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06Python\u5bf9\u8c61\u76f4\u63a5\u5199\u5165\u5230\u4e00\u4e2aJSON\u6587\u4ef6\u4e2d\u3002\u5b83\u6bd4<code>dumps()<\/code>\u66f4\u9002\u5408\u7528\u4e8e\u751f\u6210\u548c\u4fdd\u5b58JSON\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &#39;name&#39;: &#39;John Doe&#39;,<\/p>\n<p>    &#39;age&#39;: 30,<\/p>\n<p>    &#39;city&#39;: &#39;New York&#39;<\/p>\n<p>}<\/p>\n<p>with open(&#39;data.json&#39;, &#39;w&#39;) as json_file:<\/p>\n<p>    json.dump(data, json_file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>json.dump()<\/code>\u5c06<code>data<\/code>\u5b57\u5178\u76f4\u63a5\u5199\u5165\u5230<code>data.json<\/code>\u6587\u4ef6\u4e2d\uff0c\u5e76\u901a\u8fc7<code>indent<\/code>\u53c2\u6570\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7f\u751f\u6210\u7684JSON\u6587\u4ef6\u66f4\u52a0\u53ef\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528<code>dict<\/code>\u6570\u636e\u7ed3\u6784\u751f\u6210JSON<\/p>\n<\/p>\n<p><p>Python\u7684\u5b57\u5178\u6570\u636e\u7ed3\u6784\u4e0eJSON\u7684\u952e\u503c\u5bf9\u7ed3\u6784\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u5728\u521b\u5efaJSON\u6570\u636e\u65f6\uff0c\u901a\u5e38\u4f1a\u5148\u7528\u5b57\u5178\u8868\u793a\u6570\u636e\u3002\u901a\u8fc7\u5b57\u5178\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6784\u5efa\u590d\u6742\u7684JSON\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>2.1\u3001\u6784\u5efa\u7b80\u5355\u7684JSON\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u7b80\u5355\u7684JSON\u5bf9\u8c61\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5b57\u5178\u6765\u6784\u5efa\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u5d4c\u5957\u7684JSON\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {<\/p>\n<p>    &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>    &#39;age&#39;: 25,<\/p>\n<p>    &#39;address&#39;: {<\/p>\n<p>        &#39;street&#39;: &#39;123 M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n St&#39;,<\/p>\n<p>        &#39;city&#39;: &#39;Wonderland&#39;<\/p>\n<p>    },<\/p>\n<p>    &#39;skills&#39;: [&#39;Python&#39;, &#39;Data Analysis&#39;]<\/p>\n<p>}<\/p>\n<p>print(person)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2\u3001\u6784\u5efa\u590d\u6742\u7684JSON\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684JSON\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u548c\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">company = {<\/p>\n<p>    &#39;name&#39;: &#39;TechCorp&#39;,<\/p>\n<p>    &#39;employees&#39;: [<\/p>\n<p>        {&#39;name&#39;: &#39;Alice&#39;, &#39;position&#39;: &#39;Developer&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Bob&#39;, &#39;position&#39;: &#39;Manager&#39;}<\/p>\n<p>    ],<\/p>\n<p>    &#39;location&#39;: &#39;Silicon Valley&#39;,<\/p>\n<p>    &#39;products&#39;: {<\/p>\n<p>        &#39;software&#39;: [&#39;App1&#39;, &#39;App2&#39;],<\/p>\n<p>        &#39;hardware&#39;: [&#39;Device1&#39;, &#39;Device2&#39;]<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>print(company)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5d4c\u5957\u5b57\u5178\u548c\u5217\u8868\u6784\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5458\u5de5\u4fe1\u606f\u548c\u4ea7\u54c1\u4fe1\u606f\u7684\u590d\u6742JSON\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7ed3\u5408\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u751f\u6210\u548c\u7ba1\u7406JSON<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u751f\u6210\u7684JSON\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u8bfb\u53d6\u548c\u5904\u7406\u3002\u7ed3\u5408Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>3.1\u3001\u5c06JSON\u6570\u636e\u5199\u5165\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5229\u7528<code>json.dump()<\/code>\uff0c\u53ef\u4ee5\u5c06JSON\u6570\u636e\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u524d\u9762\u521b\u5efa\u7684<code>company<\/code>\u5bf9\u8c61\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>company = {<\/p>\n<p>    &#39;name&#39;: &#39;TechCorp&#39;,<\/p>\n<p>    &#39;employees&#39;: [<\/p>\n<p>        {&#39;name&#39;: &#39;Alice&#39;, &#39;position&#39;: &#39;Developer&#39;},<\/p>\n<p>        {&#39;name&#39;: &#39;Bob&#39;, &#39;position&#39;: &#39;Manager&#39;}<\/p>\n<p>    ],<\/p>\n<p>    &#39;location&#39;: &#39;Silicon Valley&#39;,<\/p>\n<p>    &#39;products&#39;: {<\/p>\n<p>        &#39;software&#39;: [&#39;App1&#39;, &#39;App2&#39;],<\/p>\n<p>        &#39;hardware&#39;: [&#39;Device1&#39;, &#39;Device2&#39;]<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>with open(&#39;company.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(company, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6JSON\u6570\u636e<\/p>\n<\/p>\n<p><p>\u8981\u8bfb\u53d6JSON\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4ece<code>company.json<\/code>\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;company.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    company_data = json.load(file)<\/p>\n<p>print(company_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>json.load()<\/code>\u5c06\u6587\u4ef6\u4e2d\u7684JSON\u6570\u636e\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u5927\u89c4\u6a21JSON\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6216\u590d\u6742\u7684JSON\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u548c\u6027\u80fd\u95ee\u9898\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u548c\u6280\u5de7\u6765\u4f18\u5316\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>4.1\u3001\u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u5927\u578bJSON\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578bJSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u9010\u884c\u5904\u7406\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def read_large_json(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            yield json.loads(line)<\/p>\n<p>for entry in read_large_json(&#39;large_data.json&#39;):<\/p>\n<p>    print(entry)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u964d\u4f4e\u5185\u5b58\u4f7f\u7528\uff0c\u5e76\u63d0\u9ad8\u5904\u7406\u5927\u6587\u4ef6\u65f6\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>4.2\u3001\u4f18\u5316JSON\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u53c2\u6570\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u7981\u7528<code>indent<\/code>\u53c2\u6570\u53ef\u4ee5\u52a0\u5feb<code>json.dump()<\/code>\u7684\u901f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {...}  # \u5047\u8bbe\u6709\u5927\u91cf\u6570\u636e<\/p>\n<p>with open(&#39;optimized.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file, separators=(&#39;,&#39;, &#39;:&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>separators<\/code>\u53c2\u6570\u53ef\u4ee5\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\uff0c\u4ece\u800c\u51cf\u5c11\u6587\u4ef6\u5927\u5c0f\u548c\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8fdb\u9636\u6280\u5de7\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u751f\u6210\u548c\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u4e00\u4e9b\u8fdb\u9636\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>5.1\u3001\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5c06\u81ea\u5b9a\u4e49Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u65f6\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u5e8f\u5217\u5316\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>class Employee:<\/p>\n<p>    def __init__(self, name, position):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.position = position<\/p>\n<p>def employee_to_dict(obj):<\/p>\n<p>    if isinstance(obj, Employee):<\/p>\n<p>        return {&#39;name&#39;: obj.name, &#39;position&#39;: obj.position}<\/p>\n<p>    raise TypeError(&#39;Object of type Employee is not JSON serializable&#39;)<\/p>\n<p>employee = Employee(&#39;Alice&#39;, &#39;Developer&#39;)<\/p>\n<p>json_string = json.dumps(employee, default=employee_to_dict)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5b9a\u4e49<code>employee_to_dict<\/code>\u51fd\u6570\u6765\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><p>5.2\u3001\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/p>\n<\/p>\n<p><p>Python\u7684<code>json<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\uff0c\u5982<code>JSONEncoder<\/code>\u7c7b\uff0c\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5e8f\u5217\u5316\u9700\u6c42\u3002\u901a\u8fc7\u7ee7\u627f<code>JSONEncoder<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316\uff1a<\/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>        if isinstance(obj, Employee):<\/p>\n<p>            return {&#39;name&#39;: obj.name, &#39;position&#39;: obj.position}<\/p>\n<p>        return super().default(obj)<\/p>\n<p>employee = Employee(&#39;Bob&#39;, &#39;Manager&#39;)<\/p>\n<p>json_string = json.dumps(employee, cls=CustomEncoder)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u65b9\u6cd5\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u7ee7\u627f<code>JSONEncoder<\/code>\u7c7b\uff0c\u7075\u6d3b\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8bb2\u89e3\uff0c\u4f60\u5e94\u8be5\u5bf9Python\u4e2d\u5982\u4f55\u81ea\u52a8\u751f\u6210JSON\u6709\u4e86\u5168\u9762\u7684\u7406\u89e3\u548c\u638c\u63e1\u3002\u65e0\u8bba\u662f\u7b80\u5355\u6570\u636e\u8fd8\u662f\u590d\u6742\u5bf9\u8c61\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u751f\u6210\u548c\u5904\u7406JSON\u6570\u636e\u3002\u5e0c\u671b\u8fd9\u4e9b\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5\u80fd\u5bf9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u7ed3\u6784\uff08\u5982\u5b57\u5178\u548c\u5217\u8868\uff09\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002\u60a8\u53ea\u9700\u4f7f\u7528<code>json.dumps()<\/code>\u51fd\u6570\uff0c\u5c06\u6570\u636e\u7ed3\u6784\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u5373\u53ef\u751f\u6210\u76f8\u5e94\u7684JSON\u5b57\u7b26\u4e32\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>json.dump()<\/code>\u53ef\u4ee5\u5c06\u6570\u636e\u76f4\u63a5\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406JSON\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\uff1f<\/strong><br \/>\u5904\u7406JSON\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u8981\u8bfb\u53d6JSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u5c06\u6587\u4ef6\u4e2d\u7684JSON\u5185\u5bb9\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u5199\u5165JSON\u6587\u4ef6\u5219\u53ef\u4ee5\u4f7f\u7528<code>json.dump()<\/code>\u51fd\u6570\uff0c\u5b83\u5c06Python\u5bf9\u8c61\u4ee5JSON\u683c\u5f0f\u5199\u5165\u6307\u5b9a\u7684\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u8bfb\u5199\u6587\u4ef6\u65f6\u4f7f\u7528\u5408\u9002\u7684\u6587\u4ef6\u6a21\u5f0f\uff08\u4f8b\u5982\uff1a&#39;r&#39;\u7528\u4e8e\u8bfb\u53d6\uff0c&#39;w&#39;\u7528\u4e8e\u5199\u5165\uff09\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684JSON\u6570\u636e\u89e3\u6790\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u6790JSON\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u9519\u8bef\u6216\u4e0d\u7b26\u5408JSON\u6807\u51c6\u7684\u60c5\u51b5\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7<code>json.JSONDecodeError<\/code>\u5f02\u5e38\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u89e3\u6790\u9519\u8bef\u800c\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u81ea\u52a8\u751f\u6210JSON\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684json\u6a21\u5757\u3001\u5229\u7528dict\u6570\u636e\u7ed3\u6784\u3001\u7ed3\u5408\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":971551,"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\/971544"}],"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=971544"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971544\/revisions"}],"predecessor-version":[{"id":971553,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971544\/revisions\/971553"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/971551"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=971544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=971544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=971544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}