{"id":985118,"date":"2024-12-27T07:34:01","date_gmt":"2024-12-26T23:34:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985118.html"},"modified":"2024-12-27T07:34:03","modified_gmt":"2024-12-26T23:34:03","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85json","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985118.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b89\u88c5json"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212424\/4f6f7336-a8f0-43ad-8188-32f6ef7c6b0d.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b89\u88c5json\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u65e0\u9700\u5355\u72ec\u5b89\u88c5JSON\u5e93<\/strong>\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u5bfc\u5165JSON\u5e93\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528<code>import json<\/code>\u5373\u53ef\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\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002Python\u5185\u7f6e\u7684JSON\u5e93\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u548c\u7c7b\uff0c\u65b9\u4fbf\u5730\u5728Python\u5bf9\u8c61\u548cJSON\u6570\u636e\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<strong>\u901a\u8fc7\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684JSON\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u89e3\u6790JSON\u6570\u636e\u3001\u751f\u6210JSON\u5b57\u7b26\u4e32\uff0c\u4ee5\u53ca\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3aJSON\u683c\u5f0f<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2dJSON\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001JSON\u6a21\u5757\u7684\u57fa\u672c\u4f7f\u7528<\/p>\n<\/p>\n<p><p>JSON\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u529f\u80fd\u6765\u5904\u7406JSON\u6570\u636e\uff0c\u5305\u62ec\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u4ee5\u53ca\u89e3\u6790JSON\u5b57\u7b26\u4e32\u4ee5\u751f\u6210Python\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u52a0\u8f7d\u548c\u89e3\u6790JSON\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u89e3\u6790JSON\u6570\u636e\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>json.loads()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u5c06JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_data = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<p>python_obj = json.loads(json_data)<\/p>\n<p>print(python_obj)<\/p>\n<p>print(type(python_obj))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86JSON\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542bJSON\u683c\u5f0f\u6570\u636e\u7684\u5b57\u7b26\u4e32\u3002\u4f7f\u7528<code>json.loads()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u4e00\u4e2aPython\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dumps()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>python_obj = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aPython\u5b57\u5178\uff0c\u5e76\u4f7f\u7528<code>json.dumps()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001JSON\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u6570\u636e\u901a\u5e38\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u9700\u8981\u5904\u7406JSON\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u3002\u4f8b\u5982\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<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\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2aJSON\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5199\u5165JSON\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u5c06Python\u5bf9\u8c61\u5199\u5165JSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dump()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06Python\u5b57\u5178\u5199\u5165\u4e00\u4e2aJSON\u6587\u4ef6\u3002\u4f7f\u7528<code>json.dump()<\/code>\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u7f29\u8fdb\u7ea7\u522b\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\uff0c\u5982<code>json.dump(data, file, indent=4)<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001JSON\u6a21\u5757\u7684\u9ad8\u7ea7\u529f\u80fd<\/p>\n<\/p>\n<ol>\n<li><strong>\u81ea\u5b9a\u4e49JSON\u7f16\u7801\u5668<\/strong><\/li>\n<\/ol>\n<p><p>JSON\u6a21\u5757\u5141\u8bb8\u60a8\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u4ee5\u5904\u7406\u7279\u6b8a\u7c7b\u578b\u7684Python\u5bf9\u8c61\u3002\u901a\u8fc7\u7ee7\u627f<code>json.JSONEncoder<\/code>\u7c7b\u5e76\u91cd\u5199<code>default()<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u5982\u4f55\u5c06\u81ea\u5b9a\u4e49\u5bf9\u8c61\u8f6c\u6362\u4e3a\u53ef\u5e8f\u5217\u5316\u683c\u5f0f\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>        if isinstance(obj, ComplexNumber):<\/p>\n<p>            return {&#39;real&#39;: obj.real, &#39;imag&#39;: obj.imag}<\/p>\n<p>        return super().default(obj)<\/p>\n<p>class ComplexNumber:<\/p>\n<p>    def __init__(self, real, imag):<\/p>\n<p>        self.real = real<\/p>\n<p>        self.imag = imag<\/p>\n<p>cn = ComplexNumber(1, 2)<\/p>\n<p>json_str = json.dumps(cn, cls=CustomEncoder)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u89e3\u6790\u81ea\u5b9a\u4e49JSON\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u60a8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u89e3\u7801\u5668\u4ee5\u89e3\u6790\u590d\u6742\u7684JSON\u5bf9\u8c61\u3002\u901a\u8fc7\u5c06<code>object_hook<\/code>\u53c2\u6570\u4f20\u9012\u7ed9<code>json.loads()<\/code>\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u5982\u4f55\u5c06JSON\u5bf9\u8c61\u8f6c\u6362\u4e3a\u81ea\u5b9a\u4e49Python\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def complex_decoder(obj):<\/p>\n<p>    if &#39;real&#39; in obj and &#39;imag&#39; in obj:<\/p>\n<p>        return ComplexNumber(obj[&#39;real&#39;], obj[&#39;imag&#39;])<\/p>\n<p>    return obj<\/p>\n<p>json_str = &#39;{&quot;real&quot;: 1, &quot;imag&quot;: 2}&#39;<\/p>\n<p>cn = json.loads(json_str, object_hook=complex_decoder)<\/p>\n<p>print(cn.real, cn.imag)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001JSON\u6a21\u5757\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>Web API\u7684\u6570\u636e\u4ea4\u6362<\/strong><\/li>\n<\/ol>\n<p><p>JSON\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u4f7f\u7528JSON\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u89e3\u6790API\u54cd\u5e94\u5e76\u751f\u6210\u8bf7\u6c42\u8d1f\u8f7d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u914d\u7f6e\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>JSON\u6587\u4ef6\u5e38\u7528\u4e8e\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u914d\u7f6e\u3002\u4f7f\u7528JSON\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8bfb\u53d6\u548c\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u5e8f\u5217\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0cJSON\u683c\u5f0f\u5e38\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u5b58\u50a8\u6a21\u578b\u53c2\u6570\u548c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u7684JSON\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5904\u7406JSON\u6570\u636e\u3002\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u5728\u5404\u79cd\u5e94\u7528\u573a\u666f\u4e2d\u6709\u6548\u5730\u4f7f\u7528JSON\u683c\u5f0f\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u3002\u65e0\u8bba\u662f\u57fa\u672c\u7684JSON\u89e3\u6790\u548c\u751f\u6210\uff0c\u8fd8\u662f\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\uff0cJSON\u6a21\u5757\u90fd\u80fd\u4e3a\u60a8\u63d0\u4f9b\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528JSON\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cJSON\u6a21\u5757\u662f\u5185\u7f6e\u7684\uff0c\u56e0\u6b64\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5b83\u5373\u53ef\u4f7f\u7528\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165JSON\u6a21\u5757\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4fbf\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528JSON\u7684\u76f8\u5173\u51fd\u6570\uff0c\u6bd4\u5982<code>json.loads()<\/code>\u548c<code>json.dumps()<\/code>\u7b49\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9bJSON\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>Python\u7684JSON\u6a21\u5757\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5217\u8868\u3001\u5b57\u5178\u548c<code>None<\/code>\u3002\u4f8b\u5982\uff0cPython\u7684\u5b57\u5178\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61\uff0c\u800cPython\u7684\u5217\u8868\u5219\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3aJSON\u6570\u7ec4\u3002\u4e86\u89e3\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u6709\u52a9\u4e8e\u5728\u5904\u7406\u6570\u636e\u65f6\u907f\u514d\u9519\u8bef\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\u4f7f\u7528<code>json.load()<\/code>\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684JSON\u6570\u636e\uff0c\u4f7f\u7528<code>json.dump()<\/code>\u5219\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u5199\u5165\u5230JSON\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\n# \u5199\u5165JSON\u6587\u4ef6\ndata = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30}\nwith open(&#39;data.json&#39;, &#39;w&#39;) as json_file:\n    json.dump(data, json_file)\n\n# \u8bfb\u53d6JSON\u6587\u4ef6\nwith open(&#39;data.json&#39;, &#39;r&#39;) as json_file:\n    loaded_data = json.load(json_file)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06Python\u5b57\u5178\u5199\u5165\u6587\u4ef6\u5e76\u518d\u8bfb\u53d6\u51fa\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u65e0\u9700\u5355\u72ec\u5b89\u88c5JSON\u5e93\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u5bfc\u5165JSON\u5e93\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":985124,"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\/985118"}],"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=985118"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985118\/revisions"}],"predecessor-version":[{"id":985128,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985118\/revisions\/985128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985124"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}