{"id":1110780,"date":"2025-01-08T17:22:57","date_gmt":"2025-01-08T09:22:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110780.html"},"modified":"2025-01-08T17:22:59","modified_gmt":"2025-01-08T09:22:59","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96json%e7%9a%84%e6%9f%90%e4%b8%80%e8%8a%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110780.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6json\u7684\u67d0\u4e00\u8282"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073456\/1ec80633-b372-4920-acc8-53b041e0e943.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6json\u7684\u67d0\u4e00\u8282\" \/><\/p>\n<p><p> <strong>Python \u63d0\u53d6 JSON \u67d0\u4e00\u8282\u7684\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u89e3\u6790 JSON \u6570\u636e\u3001\u4f7f\u7528\u7d22\u5f15\u6216\u952e\u8bbf\u95ee\u7279\u5b9a\u90e8\u5206\u3001\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6216\u5faa\u73af\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u89e3\u6790 JSON \u6570\u636e\u65f6\uff0cPython \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684 <code>json<\/code> \u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece JSON \u5bf9\u8c61\u4e2d\u63d0\u53d6\u7279\u5b9a\u90e8\u5206\u3002<strong>\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u89e3\u6790 JSON \u6570\u636e\u3001\u4f7f\u7528\u7d22\u5f15\u6216\u952e\u8bbf\u95ee\u7279\u5b9a\u90e8\u5206<\/strong>\uff0c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u8ba9\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u8981\u4ece JSON \u6570\u636e\u4e2d\u63d0\u53d6\u7279\u5b9a\u90e8\u5206\uff0c\u9996\u5148\u9700\u8981\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Python \u5b57\u5178\u6216\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528 <code>json.loads()<\/code> \u51fd\u6570\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u952e\u8bbf\u95ee\u6240\u9700\u90e8\u5206\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>JSON \u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;store&quot;: {<\/p>\n<p>        &quot;book&quot;: [<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;reference&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Nigel Rees&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sayings of the Century&quot;,<\/p>\n<p>                &quot;price&quot;: 8.95<\/p>\n<p>            },<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;fiction&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Evelyn Waugh&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sword of Honour&quot;,<\/p>\n<p>                &quot;price&quot;: 12.99<\/p>\n<p>            }<\/p>\n<p>        ],<\/p>\n<p>        &quot;bicycle&quot;: {<\/p>\n<p>            &quot;color&quot;: &quot;red&quot;,<\/p>\n<p>            &quot;price&quot;: 19.95<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u63d0\u53d6 store \u90e8\u5206<\/strong><\/h2>\n<p>store_data = data[&#39;store&#39;]<\/p>\n<p>print(store_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;book&#39;: [{&#39;category&#39;: &#39;reference&#39;, &#39;author&#39;: &#39;Nigel Rees&#39;, &#39;title&#39;: &#39;Sayings of the Century&#39;, &#39;price&#39;: 8.95}, {&#39;category&#39;: &#39;fiction&#39;, &#39;author&#39;: &#39;Evelyn Waugh&#39;, &#39;title&#39;: &#39;Sword of Honour&#39;, &#39;price&#39;: 12.99}], &#39;bicycle&#39;: {&#39;color&#39;: &#39;red&#39;, &#39;price&#39;: 19.95}}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u89e3\u6790 JSON \u6570\u636e<\/h3>\n<\/p>\n<p><p><code>json<\/code> \u6a21\u5757\u662f Python \u4e2d\u7528\u4e8e\u89e3\u6790 JSON \u6570\u636e\u7684\u6807\u51c6\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u51e0\u4e2a\u91cd\u8981\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>json.loads()<\/code>: \u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178\u6216\u5217\u8868\u3002<\/li>\n<li><code>json.load()<\/code>: \u4ece\u6587\u4ef6\u8bfb\u53d6 JSON \u6570\u636e\u5e76\u89e3\u6790\u4e3a Python \u5b57\u5178\u6216\u5217\u8868\u3002<\/li>\n<li><code>json.dumps()<\/code>: \u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>json.dump()<\/code>: \u5c06 Python \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>JSON \u5b57\u7b26\u4e32<\/strong><\/h2>\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<h2><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7d22\u5f15\u6216\u952e\u8bbf\u95ee\u7279\u5b9a\u90e8\u5206<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178\u6216\u5217\u8868\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6216\u952e\u6765\u8bbf\u95ee\u7279\u5b9a\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>JSON \u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;employees&quot;: [<\/p>\n<p>        { &quot;firstName&quot;: &quot;John&quot;, &quot;lastName&quot;: &quot;Doe&quot; },<\/p>\n<p>        { &quot;firstName&quot;: &quot;Anna&quot;, &quot;lastName&quot;: &quot;Smith&quot; },<\/p>\n<p>        { &quot;firstName&quot;: &quot;Peter&quot;, &quot;lastName&quot;: &quot;Jones&quot; }<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u63d0\u53d6 employees \u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u9879<\/strong><\/h2>\n<p>first_employee = data[&#39;employees&#39;][0]<\/p>\n<p>print(first_employee)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6216\u5faa\u73af\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6761\u4ef6\u5224\u65ad\u6216\u5faa\u73af\u6765\u63d0\u53d6 JSON \u6570\u636e\u7684\u7279\u5b9a\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>JSON \u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;employees&quot;: [<\/p>\n<p>        { &quot;firstName&quot;: &quot;John&quot;, &quot;lastName&quot;: &quot;Doe&quot;, &quot;age&quot;: 30 },<\/p>\n<p>        { &quot;firstName&quot;: &quot;Anna&quot;, &quot;lastName&quot;: &quot;Smith&quot;, &quot;age&quot;: 25 },<\/p>\n<p>        { &quot;firstName&quot;: &quot;Peter&quot;, &quot;lastName&quot;: &quot;Jones&quot;, &quot;age&quot;: 45 }<\/p>\n<p>    ]<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u63d0\u53d6\u5e74\u9f84\u5927\u4e8e30\u7684\u5458\u5de5<\/strong><\/h2>\n<p>older_employees = [employee for employee in data[&#39;employees&#39;] if employee[&#39;age&#39;] &gt; 30]<\/p>\n<p>print(older_employees)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9012\u5f52\u63d0\u53d6\u5d4c\u5957 JSON \u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0cJSON \u6570\u636e\u53ef\u80fd\u662f\u6df1\u5ea6\u5d4c\u5957\u7684\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u63d0\u53d6\u7279\u5b9a\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>JSON \u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;store&quot;: {<\/p>\n<p>        &quot;book&quot;: [<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;reference&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Nigel Rees&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sayings of the Century&quot;,<\/p>\n<p>                &quot;price&quot;: 8.95<\/p>\n<p>            },<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;fiction&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Evelyn Waugh&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sword of Honour&quot;,<\/p>\n<p>                &quot;price&quot;: 12.99<\/p>\n<p>            }<\/p>\n<p>        ],<\/p>\n<p>        &quot;bicycle&quot;: {<\/p>\n<p>            &quot;color&quot;: &quot;red&quot;,<\/p>\n<p>            &quot;price&quot;: 19.95<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u9012\u5f52\u51fd\u6570\u63d0\u53d6\u5d4c\u5957\u6570\u636e<\/strong><\/h2>\n<p>def get_nested_data(data, keys):<\/p>\n<p>    if not keys:<\/p>\n<p>        return data<\/p>\n<p>    return get_nested_data(data[keys[0]], keys[1:])<\/p>\n<h2><strong>\u63d0\u53d6 store -&gt; book -&gt; \u7b2c\u4e00\u9879 -&gt; title<\/strong><\/h2>\n<p>title = get_nested_data(data, [&#39;store&#39;, &#39;book&#39;, 0, &#39;title&#39;])<\/p>\n<p>print(title)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u590d\u6742 JSON \u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742 JSON \u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5904\u7406\u590d\u6742 JSON \u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u590d\u6742 JSON \u6570\u636e<\/strong><\/h2>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;store&quot;: {<\/p>\n<p>        &quot;book&quot;: [<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;reference&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Nigel Rees&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sayings of the Century&quot;,<\/p>\n<p>                &quot;price&quot;: 8.95,<\/p>\n<p>                &quot;av<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lability&quot;: {<\/p>\n<p>                    &quot;in_stock&quot;: true,<\/p>\n<p>                    &quot;quantity&quot;: 12<\/p>\n<p>                }<\/p>\n<p>            },<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;fiction&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Evelyn Waugh&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sword of Honour&quot;,<\/p>\n<p>                &quot;price&quot;: 12.99,<\/p>\n<p>                &quot;availability&quot;: {<\/p>\n<p>                    &quot;in_stock&quot;: false,<\/p>\n<p>                    &quot;quantity&quot;: 0<\/p>\n<p>                }<\/p>\n<p>            }<\/p>\n<p>        ],<\/p>\n<p>        &quot;bicycle&quot;: {<\/p>\n<p>            &quot;color&quot;: &quot;red&quot;,<\/p>\n<p>            &quot;price&quot;: 19.95,<\/p>\n<p>            &quot;availability&quot;: {<\/p>\n<p>                &quot;in_stock&quot;: true,<\/p>\n<p>                &quot;quantity&quot;: 3<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u63d0\u53d6\u4e66\u7c4d\u4fe1\u606f<\/strong><\/h2>\n<p>books = data[&#39;store&#39;][&#39;book&#39;]<\/p>\n<h2><strong>\u63d0\u53d6\u5e93\u5b58\u4e2d\u6709\u8d27\u7684\u4e66\u7c4d<\/strong><\/h2>\n<p>in_stock_books = [book for book in books if book[&#39;availability&#39;][&#39;in_stock&#39;]]<\/p>\n<p>print(in_stock_books)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5904\u7406 JSON \u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u7684 <code>json<\/code> \u6a21\u5757\uff0cPython \u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406 JSON \u6570\u636e\uff0c\u5982 <code>jsonpath-ng<\/code>\u3001<code>pandas<\/code> \u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u548c\u7b80\u5316\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528 <code>jsonpath-ng<\/code> \u63d0\u53d6 JSON \u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jsonpath_ng import jsonpath, parse<\/p>\n<p>import json<\/p>\n<h2><strong>JSON \u6570\u636e<\/strong><\/h2>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;store&quot;: {<\/p>\n<p>        &quot;book&quot;: [<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;reference&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Nigel Rees&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sayings of the Century&quot;,<\/p>\n<p>                &quot;price&quot;: 8.95<\/p>\n<p>            },<\/p>\n<p>            {<\/p>\n<p>                &quot;category&quot;: &quot;fiction&quot;,<\/p>\n<p>                &quot;author&quot;: &quot;Evelyn Waugh&quot;,<\/p>\n<p>                &quot;title&quot;: &quot;Sword of Honour&quot;,<\/p>\n<p>                &quot;price&quot;: 12.99<\/p>\n<p>            }<\/p>\n<p>        ],<\/p>\n<p>        &quot;bicycle&quot;: {<\/p>\n<p>            &quot;color&quot;: &quot;red&quot;,<\/p>\n<p>            &quot;price&quot;: 19.95<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<h2><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a Python \u5b57\u5178<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u4f7f\u7528 jsonpath-ng \u63d0\u53d6\u4e66\u7c4d\u6807\u9898<\/strong><\/h2>\n<p>jsonpath_expr = parse(&#39;$.store.book[*].title&#39;)<\/p>\n<p>titles = [match.value for match in jsonpath_expr.find(data)]<\/p>\n<p>print(titles)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u4ece JSON \u6570\u636e\u4e2d\u63d0\u53d6\u6240\u9700\u90e8\u5206\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u952e\u8bbf\u95ee\uff0c\u8fd8\u662f\u590d\u6742\u7684\u9012\u5f52\u63d0\u53d6\uff0cPython \u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u5e93\u6765\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6JSON\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u8f7b\u677e\u8bfb\u53d6JSON\u6587\u4ef6\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u5229\u7528<code>json.load()<\/code>\u65b9\u6cd5\u5c06\u5176\u89e3\u6790\u4e3aPython\u5b57\u5178\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u8bbf\u95eeJSON\u7684\u5404\u4e2a\u90e8\u5206\u4e86\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\nwith open(&#39;data.json&#39;, &#39;r&#39;) as file:\n    data = json.load(file)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>data<\/code>\u5c06\u5305\u542bJSON\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u7279\u5b9a\u90e8\u5206\u3002<\/p>\n<p><strong>\u5982\u4f55\u4eceJSON\u6570\u636e\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u6bb5\uff1f<\/strong><br \/>\u63d0\u53d6\u7279\u5b9a\u5b57\u6bb5\u7684\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u3002\u5047\u8bbe\u4f60\u5df2\u7ecf\u5c06JSON\u6570\u636e\u52a0\u8f7d\u5230Python\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u952e\u540d\u76f4\u63a5\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684JSON\u6570\u636e\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>&quot;name&quot;<\/code>\u7684\u5b57\u6bb5\uff0c\u53ef\u4ee5\u8fd9\u6837\u63d0\u53d6\uff1a  <\/p>\n<pre><code class=\"language-python\">name = data[&#39;name&#39;]\n<\/code><\/pre>\n<p>\u5728\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9010\u5c42\u8bbf\u95ee\u6765\u83b7\u53d6\u6240\u9700\u7684\u5b57\u6bb5\uff0c\u6bd4\u5982<code>data[&#39;user&#39;][&#39;details&#39;][&#39;age&#39;]<\/code>\u3002<\/p>\n<p><strong>\u5982\u679cJSON\u7ed3\u6784\u4e0d\u786e\u5b9a\uff0c\u5982\u4f55\u5b89\u5168\u5730\u63d0\u53d6\u6570\u636e\uff1f<\/strong><br \/>\u5f53\u5904\u7406\u7ed3\u6784\u4e0d\u786e\u5b9a\u7684JSON\u6570\u636e\u65f6\uff0c\u4f7f\u7528<code>dict.get()<\/code>\u65b9\u6cd5\u66f4\u4e3a\u5b89\u5168\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u4f60\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u4ee5\u9632\u952e\u4e0d\u5b58\u5728\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">age = data.get(&#39;user&#39;, {}).get(&#39;details&#39;, {}).get(&#39;age&#39;, &#39;\u672a\u77e5&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u9632\u6b62\u56e0\u7f3a\u5c11\u67d0\u4e2a\u5b57\u6bb5\u800c\u5bfc\u81f4\u7684\u9519\u8bef\uff0c\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u63d0\u53d6 JSON \u67d0\u4e00\u8282\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 json \u6a21\u5757\u89e3\u6790 JSON \u6570\u636e\u3001\u4f7f\u7528\u7d22\u5f15\u6216\u952e\u8bbf\u95ee\u7279\u5b9a\u90e8\u5206 [&hellip;]","protected":false},"author":3,"featured_media":1110787,"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\/1110780"}],"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=1110780"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110780\/revisions"}],"predecessor-version":[{"id":1110788,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110780\/revisions\/1110788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110787"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}