{"id":204313,"date":"2025-05-29T13:14:13","date_gmt":"2025-05-29T05:14:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204313\/"},"modified":"2025-05-29T13:14:13","modified_gmt":"2025-05-29T05:14:13","slug":"python-%e6%8a%a5%e9%94%99%ef%bc%9a%e6%97%a0%e6%b3%95%e8%a7%a3%e6%9e%90-json-%e6%95%b0%e6%8d%ae%ef%bc%8c%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204313\/","title":{"rendered":"Python \u62a5\u9519\uff1a\u65e0\u6cd5\u89e3\u6790 JSON \u6570\u636e\uff0c\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u62a5\u9519\uff1a\u65e0\u6cd5\u89e3\u6790 JSON \u6570\u636e\uff0c\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aPython \u62a5\u9519\uff1a\u65e0\u6cd5\u89e3\u6790 JSON \u6570\u636e\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/173068655667282e5c99f0e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Python \u62a5\u9519\uff1a\u65e0\u6cd5\u89e3\u6790 JSON \u6570\u636e<\/strong><\/p>\n<p>\u5728 Python \u7f16\u7a0b\u4e2d\uff0c\u9047\u5230\u5982\u95ee\u9898\u4e2d\u6240\u793a\u7684\u62a5\u9519\uff0c\u5f80\u5f80\u610f\u5473\u7740 JSON \u6570\u636e\u65e0\u6cd5\u6b63\u786e\u89e3\u6790\u3002<\/p>\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u4f20\u8f93\u7684\u683c\u5f0f\u3002\u5b83\u4f7f\u7528\u8f7b\u91cf\u7ea7\u7684\u6587\u672c\u8bed\u6cd5\u6765\u8868\u793a\u6570\u636e\u5bf9\u8c61\uff0c\u5305\u62ec\u952e\u503c\u5bf9\u548c\u6570\u7ec4\u3002<\/p>\n<p>Python \u63d0\u4f9b\u4e86 json \u6a21\u5757\uff0c\u7528\u4e8e\u89e3\u6790\u548c\u64cd\u4f5c JSON \u6570\u636e\u3002\u8be5\u6a21\u5757\u4e2d\u7528\u4e8e\u89e3\u6790 JSON \u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u662f json.loads().<\/p>\n<p>\u5f53 json.loads() \u65e0\u6cd5\u6b63\u786e\u89e3\u6790 JSON \u5b57\u7b26\u4e32\u65f6\uff0c\u5b83\u4f1a\u5f15\u53d1\u4e00\u4e2a json.JSONDecodeError \u5f02\u5e38\u3002\u9519\u8bef\u6d88\u606f\u4e2d\u901a\u5e38\u4f1a\u5305\u542b\u9519\u8bef\u7684\u5177\u4f53\u539f\u56e0\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>Syntax error<\/strong>\uff1aJSON \u5b57\u7b26\u4e32\u8bed\u6cd5\u4e0d\u6b63\u786e\u3002\u53ef\u80fd\u6709\u672a\u95ed\u5408\u7684\u5f15\u53f7\u6216\u5176\u4ed6\u8bed\u6cd5\u9519\u8bef\u3002<\/li>\n<li><strong>Type error<\/strong>\uff1aJSON \u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u4e86\u4e0d\u652f\u6301\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\u65e0\u6cd5\u89e3\u6790\u7684\u65e5\u671f\u6216\u65f6\u95f4\u3002<\/li>\n<li><strong>Data error<\/strong>\uff1aJSON \u5b57\u7b26\u4e32\u5305\u542b\u4e86\u975e\u6cd5\u5b57\u7b26\u6216\u5176\u4ed6\u6570\u636e\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u865a\u7ebf<\/strong><\/p>\n<p>\u95ee\u9898\u4e2d\u63d0\u5230\u7684\u865a\u7ebf\u901a\u5e38\u662f\u7f16\u8f91\u5668\u6216 IDE \u63d0\u4f9b\u7684\u9519\u8bef\u6307\u793a\u5668\uff0c\u8868\u793a\u4ee3\u7801\u4e2d\u5b58\u5728\u9519\u8bef\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u7a0b\u5e8f\u5458\u5feb\u901f\u627e\u5230\u5e76\u89e3\u51b3\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u9519\u8bef\uff0c\u9700\u8981\u4ed4\u7ec6\u68c0\u67e5 JSON \u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u5176\u8bed\u6cd5\u6b63\u786e\u4e14\u5305\u542b\u6709\u6548\u7684\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u68c0\u67e5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 JSON \u9a8c\u8bc1\u5de5\u5177\u5728\u7ebf\u9a8c\u8bc1 JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u4f7f\u7528 Python \u7684 json.dumps() \u65b9\u6cd5\u5c06 JSON \u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u67e5\u770b\u8f93\u51fa\uff0c\u786e\u4fdd\u5176\u683c\u5f0f\u6b63\u786e\u3002<\/li>\n<li>\u5728\u7f16\u8f91\u5668\u6216 IDE \u4e2d\u4f7f\u7528\u8bed\u6cd5\u9ad8\u4eae\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u8bed\u6cd5\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u62a5\u9519\uff1a\u65e0\u6cd5\u89e3\u6790 JSON \u6570\u636e\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u62a5\u9519\uff1a\u65e0\u6cd5\u89e3\u6790 J&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204313","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204313"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204313\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}