{"id":1086945,"date":"2025-01-08T13:30:32","date_gmt":"2025-01-08T05:30:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1086945.html"},"modified":"2025-01-08T13:30:35","modified_gmt":"2025-01-08T05:30:35","slug":"python%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90json%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1086945.html","title":{"rendered":"python\u5982\u4f55\u89e3\u6790json\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200119\/7d0435bd-7d8e-4d5d-bc87-08d611b9b540.webp\" alt=\"python\u5982\u4f55\u89e3\u6790json\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u89e3\u6790JSON\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528json\u6a21\u5757\u3001\u52a0\u8f7dJSON\u6570\u636e\u5230Python\u5bf9\u8c61\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u3001\u5904\u7406\u5d4c\u5957\u7684JSON\u7ed3\u6784\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528json\u6a21\u5757\u662f\u6700\u5e38\u7528\u548c\u4fbf\u6377\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7json.loads()\u51fd\u6570\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u89e3\u6790\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528json\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684json\u6a21\u5757\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u89e3\u6790\u548c\u5904\u7406JSON\u6570\u636e\u3002\u8981\u4f7f\u7528\u8be5\u6a21\u5757\uff0c\u9700\u8981\u5148\u8fdb\u884c\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>json.loads()\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>json.loads()\u65b9\u6cd5\u7528\u4e8e\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\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<p>python_obj = json.loads(json_string)<\/p>\n<p>print(python_obj)<\/p>\n<p>print(type(python_obj))  # \u8f93\u51fa\uff1a&lt;class &#39;dict&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>json.loads()<\/code>\u65b9\u6cd5\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5b57\u5178\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5b57\u5178\u7684\u952e\u6765\u83b7\u53d6\u76f8\u5e94\u7684\u503c\uff0c\u4f8b\u5982<code>python_obj[&#39;name&#39;]<\/code>\u5c06\u8fd4\u56de&#39;John&#39;\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>json.load()\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>json.load()\u65b9\u6cd5\u7528\u4e8e\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\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>    python_obj = json.load(file)<\/p>\n<p>print(python_obj)<\/p>\n<p>print(type(python_obj))  # \u8f93\u51fa\uff1a&lt;class &#39;dict&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>json.load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6<code>data.json<\/code>\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u89e3\u6790\u4e3aPython\u5b57\u5178\u5bf9\u8c61\u3002\u4f7f\u7528<code>with open()<\/code>\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u52a0\u8f7dJSON\u6570\u636e\u5230Python\u5bf9\u8c61<\/p>\n<\/p>\n<ol>\n<li><strong>\u89e3\u6790\u7b80\u5355\u7684JSON\u5b57\u7b26\u4e32<\/strong><\/li>\n<\/ol>\n<p><p>\u7b80\u5355\u7684JSON\u5b57\u7b26\u4e32\u5305\u542b\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u548c\u7a7a\u503c\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;is_student&quot;: false, &quot;address&quot;: null}&#39;<\/p>\n<p>python_obj = json.loads(json_string)<\/p>\n<p>print(python_obj)<\/p>\n<h2><strong>\u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;is_student&#39;: False, &#39;address&#39;: None}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cJSON\u5b57\u7b26\u4e32\u88ab\u89e3\u6790\u4e3a\u5305\u542b\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u5e03\u5c14\u503c\u548cNone\u503c\u7684Python\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u89e3\u6790\u5d4c\u5957\u7684JSON\u5b57\u7b26\u4e32<\/strong><\/li>\n<\/ol>\n<p><p>\u5d4c\u5957\u7684JSON\u5b57\u7b26\u4e32\u5305\u542b\u5d4c\u5957\u7684\u5bf9\u8c61\u548c\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;name&quot;: &quot;Bob&quot;,<\/p>\n<p>    &quot;age&quot;: 28,<\/p>\n<p>    &quot;skills&quot;: [&quot;Python&quot;, &quot;Java&quot;, &quot;C++&quot;],<\/p>\n<p>    &quot;education&quot;: {<\/p>\n<p>        &quot;degree&quot;: &quot;Bachelor&quot;,<\/p>\n<p>        &quot;university&quot;: &quot;XYZ University&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>python_obj = json.loads(json_string)<\/p>\n<p>print(python_obj)<\/p>\n<h2><strong>\u8f93\u51fa\uff1a<\/strong><\/h2>\n<h2><strong>{&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 28, &#39;skills&#39;: [&#39;Python&#39;, &#39;Java&#39;, &#39;C++&#39;], &#39;education&#39;: {&#39;degree&#39;: &#39;Bachelor&#39;, &#39;university&#39;: &#39;XYZ University&#39;}}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cJSON\u5b57\u7b26\u4e32\u88ab\u89e3\u6790\u4e3a\u5305\u542b\u5d4c\u5957\u5b57\u5178\u548c\u5217\u8868\u7684Python\u5b57\u5178\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5d4c\u5957\u7684\u952e\u6765\u83b7\u53d6\u76f8\u5e94\u7684\u503c\uff0c\u4f8b\u5982<code>python_obj[&#39;education&#39;][&#39;degree&#39;]<\/code>\u5c06\u8fd4\u56de&#39;Bachelor&#39;\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cJSON\u6570\u636e\u901a\u5e38\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5c06JSON\u6570\u636e\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;Charlie&quot;,<\/p>\n<p>    &quot;age&quot;: 35,<\/p>\n<p>    &quot;languages&quot;: [&quot;English&quot;, &quot;French&quot;, &quot;Spanish&quot;]<\/p>\n<p>}<\/p>\n<p>with open(&#39;data.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file)<\/p>\n<h2><strong>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    python_obj = json.load(file)<\/p>\n<p>print(python_obj)<\/p>\n<h2><strong>\u8f93\u51fa\uff1a<\/strong><\/h2>\n<h2><strong>{&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35, &#39;languages&#39;: [&#39;English&#39;, &#39;French&#39;, &#39;Spanish&#39;]}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06Python\u5bf9\u8c61<code>data<\/code>\u5199\u5165\u6587\u4ef6<code>data.json<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u5d4c\u5957\u7684JSON\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u5d4c\u5957\u7684JSON\u7ed3\u6784\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u5c42\u7ea7\u7684\u5bf9\u8c61\u548c\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u6216\u5faa\u73af\u7684\u65b9\u5f0f\u6765\u904d\u5386\u548c\u5904\u7406\u5d4c\u5957\u7684JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u9012\u5f52\u65b9\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u65b9\u5f0f\u904d\u5386\u548c\u5904\u7406\u5d4c\u5957\u7684JSON\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def process_json(data):<\/p>\n<p>    if isinstance(data, dict):<\/p>\n<p>        for key, value in data.items():<\/p>\n<p>            print(f&quot;Key: {key}, Value: {value}&quot;)<\/p>\n<p>            process_json(value)<\/p>\n<p>    elif isinstance(data, list):<\/p>\n<p>        for item in data:<\/p>\n<p>            process_json(item)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Value: {data}&quot;)<\/p>\n<p>json_string = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;name&quot;: &quot;David&quot;,<\/p>\n<p>    &quot;age&quot;: 40,<\/p>\n<p>    &quot;children&quot;: [<\/p>\n<p>        {<\/p>\n<p>            &quot;name&quot;: &quot;Eve&quot;,<\/p>\n<p>            &quot;age&quot;: 10<\/p>\n<p>        },<\/p>\n<p>        {<\/p>\n<p>            &quot;name&quot;: &quot;Frank&quot;,<\/p>\n<p>            &quot;age&quot;: 8<\/p>\n<p>        }<\/p>\n<p>    ],<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;city&quot;: &quot;Los Angeles&quot;,<\/p>\n<p>        &quot;state&quot;: &quot;California&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>python_obj = json.loads(json_string)<\/p>\n<p>process_json(python_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>process_json()<\/code>\u51fd\u6570\u9012\u5f52\u904d\u5386\u548c\u5904\u7406\u5d4c\u5957\u7684JSON\u6570\u636e\u3002\u6839\u636e\u6570\u636e\u7c7b\u578b\uff08\u5b57\u5178\u3001\u5217\u8868\u6216\u5176\u4ed6\uff09\uff0c\u5206\u522b\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5faa\u73af\u65b9\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u65b9\u5f0f\u904d\u5386\u548c\u5904\u7406\u5d4c\u5957\u7684JSON\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def process_json(data):<\/p>\n<p>    stack = [data]<\/p>\n<p>    while stack:<\/p>\n<p>        current = stack.pop()<\/p>\n<p>        if isinstance(current, dict):<\/p>\n<p>            for key, value in current.items():<\/p>\n<p>                print(f&quot;Key: {key}, Value: {value}&quot;)<\/p>\n<p>                stack.append(value)<\/p>\n<p>        elif isinstance(current, list):<\/p>\n<p>            for item in current:<\/p>\n<p>                stack.append(item)<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;Value: {current}&quot;)<\/p>\n<p>json_string = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;name&quot;: &quot;Grace&quot;,<\/p>\n<p>    &quot;age&quot;: 45,<\/p>\n<p>    &quot;projects&quot;: [<\/p>\n<p>        {<\/p>\n<p>            &quot;title&quot;: &quot;Project A&quot;,<\/p>\n<p>            &quot;status&quot;: &quot;Completed&quot;<\/p>\n<p>        },<\/p>\n<p>        {<\/p>\n<p>            &quot;title&quot;: &quot;Project B&quot;,<\/p>\n<p>            &quot;status&quot;: &quot;Ongoing&quot;<\/p>\n<p>        }<\/p>\n<p>    ],<\/p>\n<p>    &quot;contact&quot;: {<\/p>\n<p>        &quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;: &quot;grace@example.com&quot;,<\/p>\n<p>        &quot;phone&quot;: &quot;123-456-7890&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>python_obj = json.loads(json_string)<\/p>\n<p>process_json(python_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>process_json()<\/code>\u51fd\u6570\u4f7f\u7528\u6808\u548c\u5faa\u73af\u904d\u5386\u548c\u5904\u7406\u5d4c\u5957\u7684JSON\u6570\u636e\u3002\u6839\u636e\u6570\u636e\u7c7b\u578b\uff08\u5b57\u5178\u3001\u5217\u8868\u6216\u5176\u4ed6\uff09\uff0c\u5206\u522b\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Python\u89e3\u6790JSON\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528json\u6a21\u5757\u3001\u52a0\u8f7dJSON\u6570\u636e\u5230Python\u5bf9\u8c61\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u3001\u5904\u7406\u5d4c\u5957\u7684JSON\u7ed3\u6784\u7b49\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u64cd\u4f5cJSON\u6570\u636e\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5177\u6709\u91cd\u8981\u610f\u4e49\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\u8bfb\u53d6JSON\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\nwith open(&#39;data.json&#39;) as f:\n    data = json.load(f)\nprint(data)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06JSON\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8bfb\u53d6\u5230Python\u7684\u5b57\u5178\u6216\u5217\u8868\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>JSON\u5b57\u7b26\u4e32\u548cPython\u5b57\u5178\u4e4b\u95f4\u7684\u8f6c\u6362\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>JSON\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7<code>json.loads()<\/code>\u65b9\u6cd5\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u89e3\u6790JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684Python\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}&#39;\ndata = json.loads(json_string)\nprint(data)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u8f6c\u6362\u4f7f\u5f97\u5728Python\u4e2d\u5904\u7406\u548c\u64cd\u4f5c\u6570\u636e\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406JSON\u89e3\u6790\u65f6\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u6790JSON\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u9519\u8bef\u7b49\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7<code>json.JSONDecodeError<\/code>\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u9519\u8bef\u800c\u5d29\u6e83\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30&#39;  # \u7f3a\u5c11\u53f3\u62ec\u53f7\ntry:\n    data = json.loads(json_string)\nexcept json.JSONDecodeError as e:\n    print(&quot;\u89e3\u6790\u9519\u8bef:&quot;, e)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u8c03\u8bd5\u548c\u5904\u7406\u6f5c\u5728\u95ee\u9898\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u89e3\u6790JSON\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528json\u6a21\u5757\u3001\u52a0\u8f7dJSON\u6570\u636e\u5230Python\u5bf9\u8c61\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JS [&hellip;]","protected":false},"author":3,"featured_media":1086952,"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\/1086945"}],"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=1086945"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086945\/revisions"}],"predecessor-version":[{"id":1086955,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086945\/revisions\/1086955"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086952"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1086945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1086945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1086945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}