{"id":1083170,"date":"2025-01-08T12:55:31","date_gmt":"2025-01-08T04:55:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1083170.html"},"modified":"2025-01-08T12:55:34","modified_gmt":"2025-01-08T04:55:34","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e5%85%b8%e8%bd%ac%e4%b8%bajson%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1083170.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u5178\u8f6c\u4e3ajson\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193853\/ef085a81-7f25-4859-8ad4-693350d4fc50.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u5178\u8f6c\u4e3ajson\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u5c06\u5b57\u5178\u8f6c\u4e3aJSON\u683c\u5f0f\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u3001\u6ce8\u610f\u7f16\u7801\u548c\u89e3\u7801\u3001\u5904\u7406\u590d\u6742\u5bf9\u8c61\u3001\u4ee5\u53ca\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u5b9e\u73b0\u7f8e\u89c2\u8f93\u51fa\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8b\u5b57\u5178<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f<\/strong><\/h2>\n<p>json_data = json.dumps(data)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>json.dumps()<\/code>\u65b9\u6cd5\u4f1a\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u4e00\u4e2aJSON\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u6765\u7f8e\u89c2\u5730\u8f93\u51faJSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f8e\u89c2\u8f93\u51faJSON<\/p>\n<p>json_data_pretty = json.dumps(data, indent=4)<\/p>\n<p>print(json_data_pretty)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>json<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5904\u7406JSON\u6570\u636e\u3002\u8981\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u4e2d\u7684<code>dumps()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f1a\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<p>json_data = json.dumps(data)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>json<\/code>\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e00\u4e9b\u952e\u503c\u5bf9\u7684\u5b57\u5178\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>json.dumps()<\/code>\u65b9\u6cd5\u662f\u5c06Python\u5bf9\u8c61\uff08\u5982\u5b57\u5178\uff09\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u5b83\u63a5\u53d7\u5404\u79cd\u53c2\u6570\uff0c\u5141\u8bb8\u6211\u4eec\u63a7\u5236JSON\u8f93\u51fa\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_data = json.dumps(data, indent=4, separators=(&quot;,&quot;, &quot;: &quot;), sort_keys=True)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e09\u4e2a\u989d\u5916\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>indent<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u7f29\u8fdb\u7ea7\u522b\uff0c\u4f7f\u8f93\u51fa\u7684JSON\u66f4\u52a0\u7f8e\u89c2\u3002<\/li>\n<li><code>separators<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u952e\u503c\u5bf9\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002<\/li>\n<li><code>sort_keys<\/code>\u53c2\u6570\u7528\u4e8e\u5c06\u952e\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u6ce8\u610f\u7f16\u7801\u548c\u89e3\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7f16\u7801\u548c\u89e3\u7801\u7684\u95ee\u9898\u3002<code>json.dumps()<\/code>\u65b9\u6cd5\u4f1a\u5c06Python\u5bf9\u8c61\u7f16\u7801\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u800c<code>json.loads()<\/code>\u65b9\u6cd5\u5219\u4f1a\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u7801\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/p>\n<p>json_data = json.dumps(data)<\/p>\n<h2><strong>\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u5b57\u5178<\/strong><\/h2>\n<p>data_dict = json.loads(json_data)<\/p>\n<p>print(data_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u56de\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u590d\u6742\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u5305\u542b\u590d\u6742\u5bf9\u8c61\u7684\u5b57\u5178\uff0c\u5982\u81ea\u5b9a\u4e49\u7c7b\u5b9e\u4f8b\u3002<code>json<\/code>\u6a21\u5757\u65e0\u6cd5\u76f4\u63a5\u5904\u7406\u8fd9\u4e9b\u5bf9\u8c61\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u7f16\u7801\u548c\u89e3\u7801\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>def person_encoder(obj):<\/p>\n<p>    if isinstance(obj, Person):<\/p>\n<p>        return {&quot;name&quot;: obj.name, &quot;age&quot;: obj.age}<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se TypeError(&quot;Object of type Person is not JSON serializable&quot;)<\/p>\n<p>person = Person(&quot;Alice&quot;, 25)<\/p>\n<p>json_data = json.dumps(person, default=person_encoder)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Person<\/code>\u7c7b\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u5b9e\u4f8b\u3002\u4e3a\u4e86\u5c06<code>Person<\/code>\u5b9e\u4f8b\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>person_encoder<\/code>\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9<code>json.dumps()<\/code>\u65b9\u6cd5\u7684<code>default<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u5b9e\u73b0\u7f8e\u89c2\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u4ee5\u7f8e\u89c2\u7684\u683c\u5f0f\u8f93\u51faJSON\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>indent<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u7f29\u8fdb\u7ea7\u522b\uff0c\u4ece\u800c\u4f7f\u8f93\u51fa\u7684JSON\u66f4\u52a0\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_data_pretty = json.dumps(data, indent=4)<\/p>\n<p>print(json_data_pretty)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>indent<\/code>\u53c2\u6570\u5c06JSON\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u66f4\u52a0\u7f8e\u89c2\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u548c\u975eASCII\u5b57\u7b26<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u548c\u975eASCII\u5b57\u7b26\u7684\u5b57\u5178\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7f16\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>ensure_ascii<\/code>\u53c2\u6570\u6765\u63a7\u5236\u662f\u5426\u5c06\u975eASCII\u5b57\u7b26\u7f16\u7801\u4e3a<code>\\\\uXXXX<\/code>\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_with_special_chars = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;city&quot;: &quot;M\u00fcnchen&quot;,<\/p>\n<p>    &quot;emoji&quot;: &quot;\ud83d\ude0a&quot;<\/p>\n<p>}<\/p>\n<p>json_data = json.dumps(data_with_special_chars, ensure_ascii=False)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>ensure_ascii<\/code>\u53c2\u6570\u6765\u63a7\u5236\u662f\u5426\u5c06\u975eASCII\u5b57\u7b26\u7f16\u7801\u4e3a<code>\\\\uXXXX<\/code>\u5e8f\u5217\u3002\u901a\u8fc7\u8bbe\u7f6e<code>ensure_ascii=False<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4fdd\u7559\u539f\u59cb\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u7684\u5b57\u5178\u65f6\uff0c<code>json<\/code>\u6a21\u5757\u65e0\u6cd5\u76f4\u63a5\u5e8f\u5217\u5316\u8fd9\u4e9b\u5bf9\u8c61\u3002\u9700\u8981\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u7f16\u7801\u51fd\u6570\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>data_with_datetime = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;timestamp&quot;: datetime.now()<\/p>\n<p>}<\/p>\n<p>def datetime_encoder(obj):<\/p>\n<p>    if isinstance(obj, datetime):<\/p>\n<p>        return obj.isoformat()<\/p>\n<p>    raise TypeError(&quot;Object of type datetime is not JSON serializable&quot;)<\/p>\n<p>json_data = json.dumps(data_with_datetime, default=datetime_encoder)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u7684\u5b57\u5178\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a<code>datetime_encoder<\/code>\u51fd\u6570\u6765\u5904\u7406<code>datetime<\/code>\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06<code>datetime_encoder<\/code>\u51fd\u6570\u4f20\u9012\u7ed9<code>json.dumps()<\/code>\u65b9\u6cd5\u7684<code>default<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u548c\u5199\u5165JSON<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u5b57\u5178\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u3002<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u5178\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d<\/p>\n<p>with open(&quot;data.json&quot;, &quot;w&quot;) as file:<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<h2><strong>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e<\/strong><\/h2>\n<p>with open(&quot;data.json&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data_from_file = json.load(file)<\/p>\n<p>print(data_from_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>json.dump()<\/code>\u65b9\u6cd5\u5c06\u5b57\u5178\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u5e76\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5904\u7406\u5d4c\u5957\u5b57\u5178\u548c\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u5d4c\u5957\u5b57\u5178\u548c\u5217\u8868\u7684\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c<code>json<\/code>\u6a21\u5757\u4e5f\u80fd\u591f\u5f88\u597d\u5730\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_data = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/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;postal_code&quot;: &quot;10001&quot;<\/p>\n<p>    },<\/p>\n<p>    &quot;hobbies&quot;: [&quot;reading&quot;, &quot;traveling&quot;, &quot;swimming&quot;]<\/p>\n<p>}<\/p>\n<p>json_data = json.dumps(nested_data, indent=4)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u5d4c\u5957\u5b57\u5178\u548c\u5217\u8868\u7684\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u4f7f\u7528<code>json.dumps()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u5904\u7406\u5927\u6570\u636e\u548c\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528<code>orjson<\/code>\u6216<code>ujson<\/code>\u7b49\u7b2c\u4e09\u65b9\u5e93\u6765\u63d0\u9ad8\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import orjson<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u4f7f\u7528orjson\u8fdb\u884c\u5e8f\u5217\u5316<\/strong><\/h2>\n<p>json_data = orjson.dumps(data).decode()<\/p>\n<p>print(json_data)<\/p>\n<h2><strong>\u4f7f\u7528orjson\u8fdb\u884c\u53cd\u5e8f\u5217\u5316<\/strong><\/h2>\n<p>data_dict = orjson.loads(json_data)<\/p>\n<p>print(data_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>orjson<\/code>\u5e93\u6765\u63d0\u9ad8\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u5341\u4e00\u3001\u5904\u7406\u81ea\u5b9a\u4e49\u7c7b\u548c\u6570\u636e\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u81ea\u5b9a\u4e49\u7c7b\u548c\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u7f16\u7801\u548c\u89e3\u7801\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u7c7b\u7684<code>__dict__<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u5c5e\u6027\u5b57\u5178\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def to_json(self):<\/p>\n<p>        return json.dumps(self.__dict__)<\/p>\n<p>person = Person(&quot;Alice&quot;, 25)<\/p>\n<p>json_data = person.to_json()<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Person<\/code>\u7c7b\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e00\u4e2a<code>to_json<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u7c7b\u7684<code>__dict__<\/code>\u5c5e\u6027\u5c06\u5bf9\u8c61\u7684\u5c5e\u6027\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u901a\u8fc7\u4f7f\u7528Python\u7684<code>json<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5e76\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528<code>json.dumps()<\/code>\u3001<code>json.loads()<\/code>\u3001<code>json.dump()<\/code>\u548c<code>json.load()<\/code>\u65b9\u6cd5\uff0c\u4ee5\u53ca\u81ea\u5b9a\u4e49\u7f16\u7801\u548c\u89e3\u7801\u51fd\u6570\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570JSON\u5904\u7406\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u8f7b\u677e\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u3002\u901a\u8fc7\u8c03\u7528<code>json.dumps()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5b57\u5178\u5bf9\u8c61\u8f6c\u5316\u4e3aJSON\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\ndata = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}\njson_data = json.dumps(data)\nprint(json_data)  # \u8f93\u51fa: {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}\n<\/code><\/pre>\n<p><strong>\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u540e\uff0c\u5982\u4f55\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5c06\u5b57\u5178\u8f6c\u6362\u540e\u7684JSON\u683c\u5f0f\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dump()<\/code>\u65b9\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u624b\u52a8\u5904\u7406\u5b57\u7b26\u4e32\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, &#39;city&#39;: &#39;New York&#39;}\nwith open(&#39;data.json&#39;, &#39;w&#39;) as json_file:\n    json.dump(data, json_file)\n<\/code><\/pre>\n<p><strong>\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u590d\u6742\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u53ef\u80fd\u5305\u542b\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3001\u65e5\u671f\u7b49\u3002\u4e3a\u4e86\u5c06\u8fd9\u4e9b\u7c7b\u578b\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e2a\u5e8f\u5217\u5316\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9<code>json.dumps()<\/code>\u6216<code>json.dump()<\/code>\u7684<code>default<\/code>\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import json\nfrom datetime import datetime\n\nclass Person:\n    def __init__(self, name, birthdate):\n        self.name = name\n        self.birthdate = birthdate\n\ndef serialize(obj):\n    if isinstance(obj, datetime):\n        return obj.isoformat()\n    if isinstance(obj, Person):\n        return {&#39;name&#39;: obj.name, &#39;birthdate&#39;: obj.birthdate.isoformat()}\n    raise TypeError(f&#39;Type {type(obj)} not serializable&#39;)\n\nperson = Person(&#39;Alice&#39;, datetime(1993, 5, 17))\njson_data = json.dumps(person, default=serialize)\nprint(json_data)  # \u8f93\u51fa: {&quot;name&quot;: &quot;Alice&quot;, &quot;birthdate&quot;: &quot;1993-05-17T00:00:00&quot;}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u5b57\u5178\u8f6c\u4e3aJSON\u683c\u5f0f\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528json\u6a21\u5757\u3001\u4f7f\u7528json.dumps()\u65b9\u6cd5\u3001\u6ce8\u610f\u7f16\u7801\u548c [&hellip;]","protected":false},"author":3,"featured_media":1083179,"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\/1083170"}],"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=1083170"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083170\/revisions"}],"predecessor-version":[{"id":1083180,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083170\/revisions\/1083180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083179"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1083170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1083170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1083170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}