{"id":940283,"date":"2024-12-26T20:57:00","date_gmt":"2024-12-26T12:57:00","guid":{"rendered":""},"modified":"2024-12-26T20:57:02","modified_gmt":"2024-12-26T12:57:02","slug":"python-json%e5%a6%82%e4%bd%95%e6%88%aa%e5%8f%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/940283.html","title":{"rendered":"python json\u5982\u4f55\u622a\u53d6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074955\/e64b03b8-5550-4f4a-974b-75d5c0e01432.webp\" alt=\"python json\u5982\u4f55\u622a\u53d6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u622a\u53d6JSON\u6570\u636e\u901a\u5e38\u6d89\u53ca\u89e3\u6790\u548c\u64cd\u4f5cJSON\u683c\u5f0f\u7684\u6570\u636e\u3002<strong>\u89e3\u6790JSON\u5b57\u7b26\u4e32\u3001\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u3001\u4fee\u6539\u6570\u636e<\/strong>\uff0c\u662f\u5904\u7406JSON\u6570\u636e\u7684\u5e38\u89c1\u4efb\u52a1\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u5e76\u5206\u4eab\u4e00\u4e9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u89e3\u6790JSON\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u89e3\u6790JSON\u5b57\u7b26\u4e32\u3002JSON\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u7c7b\u4f3c\u4e8ePython\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8bJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_string = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<h2><strong>\u89e3\u6790JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>data = json.loads(json_string)<\/p>\n<p>print(data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u89e3\u6790\u540e\u7684\u6570\u636e\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u540d\u8bbf\u95ee\u5176\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u63d0\u53d6\u7279\u5b9a\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u89e3\u6790JSON\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u64cd\u4f5c\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1. \u63d0\u53d6\u5355\u4e2a\u503c<\/h4>\n<\/p>\n<p><p>\u8981\u63d0\u53d6\u7279\u5b9a\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u952e\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = data[&#39;name&#39;]<\/p>\n<p>print(name)  # \u8f93\u51fa: John<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u63d0\u53d6\u5d4c\u5957\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5f53JSON\u5305\u542b\u5d4c\u5957\u7ed3\u6784\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u94fe\u5f0f\u952e\u540d\u8bbf\u95ee\u5d4c\u5957\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_json_string = &#39;{&quot;person&quot;: {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<p>nested_data = json.loads(nested_json_string)<\/p>\n<p>age = nested_data[&#39;person&#39;][&#39;age&#39;]<\/p>\n<p>print(age)  # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>JSON\u53ef\u4ee5\u5305\u542b\u6570\u7ec4\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array_json_string = &#39;{&quot;names&quot;: [&quot;John&quot;, &quot;Jane&quot;, &quot;Doe&quot;]}&#39;<\/p>\n<p>array_data = json.loads(array_json_string)<\/p>\n<p>first_name = array_data[&#39;names&#39;][0]<\/p>\n<p>print(first_name)  # \u8f93\u51fa: John<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4fee\u6539JSON\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u4fee\u6539JSON\u6570\u636e\uff0c\u5728\u89e3\u6790\u4e3a\u5b57\u5178\u540e\uff0c\u53ef\u4ee5\u50cf\u64cd\u4f5c\u666e\u901a\u5b57\u5178\u4e00\u6837\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fee\u6539\u5355\u4e2a\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">data[&#39;age&#39;] = 31<\/p>\n<p>print(data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 31, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6dfb\u52a0\u65b0\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">data[&#39;country&#39;] = &#39;USA&#39;<\/p>\n<p>print(data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 31, &#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5220\u9664\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">del data[&#39;city&#39;]<\/p>\n<p>print(data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 31, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u4fee\u6539\u540e\uff0c\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u56deJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_output = json.dumps(data)<\/p>\n<p>print(json_output)  # \u8f93\u51fa: {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 31, &quot;country&quot;: &quot;USA&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>json.dumps()<\/code>\uff0c\u53ef\u4ee5\u5c06Python\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd8\u63d0\u4f9b\u4e86\u683c\u5f0f\u5316\u9009\u9879\uff0c\u6bd4\u5982\u7f29\u8fdb\u548c\u6392\u5e8f\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u590d\u6742JSON\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u6570\u636e\u53ef\u80fd\u4f1a\u66f4\u52a0\u590d\u6742\uff0c\u5305\u62ec\u66f4\u6df1\u7684\u5d4c\u5957\u7ed3\u6784\u548c\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u590d\u6742\u5d4c\u5957\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u53ef\u4ee5\u9012\u5f52\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">complex_json_string = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls&quot;: {<\/p>\n<p>        &quot;age&quot;: 30,<\/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>        }<\/p>\n<p>    },<\/p>\n<p>    &quot;hobbies&quot;: [&quot;reading&quot;, &quot;traveling&quot;]<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>complex_data = json.loads(complex_json_string)<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u6570\u636e<\/strong><\/h2>\n<p>street = complex_data[&#39;details&#39;][&#39;address&#39;][&#39;street&#39;]<\/p>\n<p>print(street)  # \u8f93\u51fa: 123 Main St<\/p>\n<h2><strong>\u4fee\u6539\u5d4c\u5957\u6570\u636e<\/strong><\/h2>\n<p>complex_data[&#39;details&#39;][&#39;address&#39;][&#39;city&#39;] = &#39;Los Angeles&#39;<\/p>\n<p>print(complex_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u904d\u5386JSON\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u904d\u5386JSON\u4e2d\u7684\u6570\u7ec4\u548c\u5bf9\u8c61\uff0c\u4ee5\u8bbf\u95ee\u6216\u4fee\u6539\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u6570\u7ec4<\/p>\n<p>for hobby in complex_data[&#39;hobbies&#39;]:<\/p>\n<p>    print(hobby)<\/p>\n<h2><strong>\u904d\u5386\u5bf9\u8c61<\/strong><\/h2>\n<p>for key, value in complex_data[&#39;details&#39;].items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5b57\u7b26\u4e32\uff0cJSON\u6570\u636e\u4e5f\u7ecf\u5e38\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0cPython\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    file_data = json.load(file)<\/p>\n<p>print(file_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5199\u5165JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8bfb\u5199JSON\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.load()<\/code>\u548c<code>json.dump()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5206\u522b\u7528\u4e8e\u4ece\u6587\u4ef6\u8bfb\u53d6\u548c\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5904\u7406JSON\u6570\u636e\u662fPython\u5f00\u53d1\u4e2d\u7684\u5e38\u89c1\u4efb\u52a1\uff0c\u7406\u89e3\u5982\u4f55\u89e3\u6790\u3001\u63d0\u53d6\u3001\u4fee\u6539\u548c\u5b58\u50a8JSON\u6570\u636e\u662f\u975e\u5e38\u91cd\u8981\u7684\u6280\u80fd\u3002\u901a\u8fc7<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9JSON\u6570\u636e\u7684\u5404\u79cd\u64cd\u4f5c\u3002\u5728\u590d\u6742\u9879\u76ee\u4e2d\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5230API\u6570\u636e\u4ea4\u6362\u65f6\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5c06\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u89e3\u6790JSON\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u89e3\u6790JSON\u6570\u636e\u901a\u5e38\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\njson_data = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}&#39;\ndata = json.loads(json_data)\nprint(data[&quot;name&quot;])  # \u8f93\u51fa: Alice\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95eeJSON\u6570\u636e\u4e2d\u7684\u5404\u4e2a\u5b57\u6bb5\u3002<\/p>\n<p><strong>\u5982\u4f55\u4eceJSON\u5bf9\u8c61\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u6bb5\uff1f<\/strong><br \/>\u63d0\u53d6\u7279\u5b9a\u5b57\u6bb5\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u952e\u540d\u76f4\u63a5\u8bbf\u95ee\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2aJSON\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u4e8e<code>data[&#39;key&#39;]<\/code>\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">user_info = &#39;{&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}&#39;\ninfo = json.loads(user_info)\n\nname = info[&#39;name&#39;]  # \u63d0\u53d6\u540d\u5b57\nage = info[&#39;age&#39;]    # \u63d0\u53d6\u5e74\u9f84\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u60a8\u80fd\u591f\u7075\u6d3b\u5730\u4ece\u590d\u6742\u7684JSON\u7ed3\u6784\u4e2d\u63d0\u53d6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406JSON\u6570\u7ec4\u4e2d\u7684\u591a\u4e2a\u5bf9\u8c61\uff1f<\/strong><br \/>\u5f53JSON\u6570\u636e\u4e2d\u5305\u542b\u6570\u7ec4\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u6bd4\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u7528\u6237\u4fe1\u606f\u7684JSON\u6570\u7ec4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u8fd9\u4e9b\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">json_array = &#39;[{&quot;name&quot;: &quot;Charlie&quot;}, {&quot;name&quot;: &quot;Dave&quot;}]&#39;\nusers = json.loads(json_array)\n\nfor user in users:\n    print(user[&#39;name&#39;])  # \u8f93\u51fa: Charlie \u548c Dave\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u60a8\u5904\u7406\u548c\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u622a\u53d6JSON\u6570\u636e\u901a\u5e38\u6d89\u53ca\u89e3\u6790\u548c\u64cd\u4f5cJSON\u683c\u5f0f\u7684\u6570\u636e\u3002\u89e3\u6790JSON\u5b57\u7b26\u4e32\u3001\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u3001\u4fee\u6539 [&hellip;]","protected":false},"author":3,"featured_media":940287,"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\/940283"}],"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=940283"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940283\/revisions"}],"predecessor-version":[{"id":940289,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940283\/revisions\/940289"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/940287"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=940283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=940283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=940283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}