{"id":50714,"date":"2024-12-03T16:49:55","date_gmt":"2024-12-03T08:49:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50714\/"},"modified":"2024-12-03T16:49:55","modified_gmt":"2024-12-03T08:49:55","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-python-%e4%b8%ad%e7%9a%84-json-%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba-listdict%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50714\/","title":{"rendered":"\u5982\u4f55\u5c06 Python \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a List[Dict]\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5c06 Python \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a List[Dict]\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/17325916756745403b12de5.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06 Python \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a List[Dict]\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06 Python \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a List[Dict]\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 python \u4e2d\u5c06 string \u8f6c\u6362\u4e3a list[dict]<\/strong><\/p>\n<p>\u4f60\u60f3\u5c06\u4e00\u4e2a\u5305\u542b json \u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210 python \u4e2d\u7684 list[dict]\u3002\u8981\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528 json.loads() \u51fd\u6570\u3002<\/p>\n<pre>import json\n\nstr_content = '''\n[\n{\n\"Id\": \"Failure\",\n\"Description\": null,\n\"Event\": [],\n\"RowId\": 1\n},\n{\n\"Id\": \"Virtual\",\n\"Description\": null,\n\"Event\": [],\n\"RowId\": 2\n},\n{\n\"Id\": \"Cleaning\",\n\"Description\": null,\n\"Event\": [],\n\"RowId\": 3\n}\n]\n'''\n\n# \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a list[dict]\nexpected_result_converted = json.loads(str_content)<\/pre>\n<p>json.loads() \u51fd\u6570\u5c06 json \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a python \u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u5305\u62ec dict\u3001list \u548c\u5176\u4ed6\u539f\u8bed\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cjson \u5b57\u7b26\u4e32\u662f\u4e00\u4e2a list\uff0c\u56e0\u6b64\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u5305\u542b dict \u7684 list\u3002<\/p>\n<p>\u8f6c\u6362\u540e\uff0cexpected_result_converted \u5c06\u662f\u4e00\u4e2a list\uff0c\u5176\u5143\u7d20\u662f dict\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u636e\uff0c\u5c31\u50cf\u4f60\u64cd\u4f5c\u5176\u4ed6 python \u6570\u636e\u7ed3\u6784\u4e00\u6837\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5c06 Python \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a List[Dict]\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 Python \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a List[Dict]\uff1f \u5728 python \u4e2d\u5c06 string \u8f6c\u6362\u4e3a list[dict] \u4f60\u60f3\u5c06\u4e00\u4e2a\u5305\u542b json \u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210 python \u4e2d\u7684 list[dict]\u3002\u8981\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528 json.loads() \u51fd\u6570\u3002 import json str_content = &#8221;&#8217; [ { &#8220;Id&#8221;: &#8220;Failure&#8221;, &#8220;Description&#8221;: null, &#8220;Event&#8221;: [], &#8220;RowId&#8221;: 1 }, { &#8220;Id&#8221;: &#8220;Virtual&#8221;, &#8220;Description&#8221;: null, &#8220;Event&#8221;: [], &#8220;RowId&#8221;: 2 }, { &#8220;Id&#8221;: &#8220;Cleaning&#8221;, &#8220;Description&#8221;: null, &#8220;Event&#8221;: [], &#8220;RowId&#8221;: 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50714","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50714"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50714\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}