{"id":962063,"date":"2024-12-27T04:04:47","date_gmt":"2024-12-26T20:04:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962063.html"},"modified":"2024-12-27T04:04:49","modified_gmt":"2024-12-26T20:04:49","slug":"python%e5%a6%82%e4%bd%95%e8%af%bbjson%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962063.html","title":{"rendered":"python\u5982\u4f55\u8bfbjson\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104057\/6ef3105c-ac8c-40c5-be3c-82e75b7330a1.webp\" alt=\"python\u5982\u4f55\u8bfbjson\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6JSON\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684json\u6a21\u5757\u3001\u7528pandas\u5e93\u8bfb\u53d6JSON\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982simplejson\u3001\u5904\u7406\u5927\u6587\u4ef6\u65f6\u4f7f\u7528\u9010\u884c\u89e3\u6790\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u7684json\u6a21\u5757\u6765\u8bfb\u53d6JSON\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6ejson\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u65b9\u6cd5\u6765\u5904\u7406JSON\u6570\u636e\u3002\u8981\u8bfb\u53d6JSON\u6587\u4ef6\uff0c\u901a\u5e38\u7684\u6b65\u9aa4\u662f\u6253\u5f00\u6587\u4ef6\u3001\u52a0\u8f7dJSON\u6570\u636e\u5e76\u89e3\u6790\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5185\u7f6ejson\u6a21\u5757\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>Python\u7684json\u6a21\u5757\u63d0\u4f9b\u4e86<code>load()<\/code>\u65b9\u6cd5\u6765\u8bfb\u53d6JSON\u6587\u4ef6\u3002\u9996\u5148\u9700\u8981\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\uff08\u901a\u5e38\u662f\u5b57\u5178\u6216\u5217\u8868\uff09\u3002<\/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>    data = json.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528<code>with open()<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728\u6587\u4ef6\u5904\u7406\u5b8c\u6210\u540e\u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u907f\u514d\u56e0\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u9732\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001JSON\u6587\u4ef6\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u7c7b\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u57fa\u4e8eJavaScript\u7f16\u7a0b\u8bed\u8a00\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u91c7\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684\u6587\u672c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001JSON\u7684\u57fa\u672c\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>JSON\u7684\u57fa\u672c\u7ed3\u6784\u5305\u62ec\u4e24\u79cd\uff1a\u5bf9\u8c61\uff08object\uff09\u548c\u6570\u7ec4\uff08array\uff09\u3002\u5bf9\u8c61\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u8868\u793a\uff1b\u6570\u7ec4\u662f\u503c\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u8868\u793a\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u5bf9\u8c61\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u6570\u7ec4\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">[<\/p>\n<p>    &quot;apple&quot;,<\/p>\n<p>    &quot;banana&quot;,<\/p>\n<p>    &quot;cherry&quot;<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2\u3001JSON\u4e0ePython\u7684\u6570\u636e\u7c7b\u578b\u6620\u5c04<\/h4>\n<\/p>\n<p><p>JSON\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u4e0ePython\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u6709\u76f4\u63a5\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u8fd9\u4f7f\u5f97\u5728Python\u4e2d\u5904\u7406JSON\u6570\u636e\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<ul>\n<li>JSON\u5bf9\u8c61 -&gt; Python\u5b57\u5178<\/li>\n<li>JSON\u6570\u7ec4 -&gt; Python\u5217\u8868<\/li>\n<li>JSON\u5b57\u7b26\u4e32 -&gt; Python\u5b57\u7b26\u4e32<\/li>\n<li>JSON\u6570\u5b57 -&gt; Python\u6574\u6570\u6216\u6d6e\u70b9\u6570<\/li>\n<li>JSON\u5e03\u5c14\u503c -&gt; Python\u5e03\u5c14\u503c<\/li>\n<li>JSON\u7a7a\u503c\uff08null\uff09 -&gt; Python None<\/li>\n<\/ul>\n<hr>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u7684json\u6a21\u5757\u8bfb\u53d6JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Python\u7684json\u6a21\u5757\u662f\u5904\u7406JSON\u6570\u636e\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u51fd\u6570\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8981\u8bfb\u53d6JSON\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>json.load()<\/code>\u51fd\u6570\u5c06\u5176\u5185\u5bb9\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002<\/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>    data = json.load(file)<\/p>\n<h2><strong>\u8f93\u51fa\u89e3\u6790\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cJSON\u6587\u4ef6\u88ab\u8bfb\u53d6\u5e76\u89e3\u6790\u4e3a\u4e00\u4e2aPython\u5b57\u5178\u6216\u5217\u8868\uff0c\u5177\u4f53\u53d6\u51b3\u4e8eJSON\u6587\u4ef6\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790JSON\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709\u4e00\u4e2aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.loads()<\/code>\u51fd\u6570\u89e3\u6790\u5b83\u3002<\/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>data = json.loads(json_string)<\/p>\n<h2><strong>\u8f93\u51fa\u89e3\u6790\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u6765\u81eaAPI\u6216\u5176\u4ed6\u7f51\u7edc\u8bf7\u6c42\u7684JSON\u54cd\u5e94\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u8bfb\u53d6JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u7684json\u6a21\u5757\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6JSON\u6587\u4ef6\uff0c\u5982pandas\u3001simplejson\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528pandas\u5e93<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u652f\u6301\u76f4\u63a5\u8bfb\u53d6JSON\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\uff0c\u8fd9\u5728\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_json(&#39;data.json&#39;)<\/p>\n<h2><strong>\u663e\u793aDataFrame\u7684\u524d\u51e0\u884c<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684<code>read_json()<\/code>\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684JSON\u7ed3\u6784\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5408\u5206\u6790\u7684\u8868\u683c\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528simplejson\u5e93<\/h4>\n<\/p>\n<p><p>simplejson\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u6bd4\u5185\u7f6ejson\u6a21\u5757\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406JSON\u6570\u636e\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import simplejson as json<\/p>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>simplejson\u7684\u4f7f\u7528\u65b9\u6cd5\u4e0e\u5185\u7f6e\u7684json\u6a21\u5757\u51e0\u4e4e\u4e00\u81f4\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\u53ef\u80fd\u4f1a\u6709\u6027\u80fd\u4e0a\u7684\u4f18\u52bf\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u56db\u3001\u5904\u7406\u5927\u89c4\u6a21JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21JSON\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u89e3\u6790\u7684\u65b9\u6cd5\uff0c\u9010\u6b65\u8bfb\u53d6\u548c\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9010\u884c\u89e3\u6790JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u578bJSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u8003\u8651\u9010\u884c\u8bfb\u53d6\u548c\u89e3\u6790\u6587\u4ef6\u5185\u5bb9\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def read_large_json(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            yield json.loads(line)<\/p>\n<p>for item in read_large_json(&#39;large_data.json&#39;):<\/p>\n<p>    # \u5904\u7406\u6bcf\u4e00\u884c\u7684JSON\u5bf9\u8c61<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8eJSON\u6587\u4ef6\u4e2d\u6bcf\u4e00\u884c\u90fd\u662f\u72ec\u7acb\u7684JSON\u5bf9\u8c61\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5206\u5757\u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5904\u7406\u5927\u6587\u4ef6\u7684\u65b9\u6cd5\u662f\u5206\u5757\u8bfb\u53d6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6574\u4e2a\u6587\u4ef6\u662f\u4e00\u4e2a\u5927\u7684JSON\u5bf9\u8c61\u6216\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def read_json_in_chunks(file_path, chunk_size=1000):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        buffer = []<\/p>\n<p>        for line in file:<\/p>\n<p>            buffer.append(line.strip())<\/p>\n<p>            if len(buffer) &gt;= chunk_size:<\/p>\n<p>                yield json.loads(&#39;&#39;.join(buffer))<\/p>\n<p>                buffer = []<\/p>\n<p>    if buffer:<\/p>\n<p>        yield json.loads(&#39;&#39;.join(buffer))<\/p>\n<p>for chunk in read_json_in_chunks(&#39;large_data.json&#39;):<\/p>\n<p>    # \u5904\u7406\u6bcf\u4e2a\u5757\u7684JSON\u5bf9\u8c61<\/p>\n<p>    print(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6587\u4ef6\u7ed3\u6784\u8fdb\u884c\u8c03\u6574\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5757\u90fd\u662f\u5b8c\u6574\u7684JSON\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u5982\u7f16\u7801\u9519\u8bef\u3001\u6587\u4ef6\u635f\u574f\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u7f16\u7801\u9519\u8bef<\/h4>\n<\/p>\n<p><p>JSON\u6587\u4ef6\u901a\u5e38\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4f46\u6709\u65f6\u6587\u4ef6\u53ef\u80fd\u4f7f\u7528\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u3002\u53ef\u4ee5\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6587\u4ef6\u635f\u574f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u8bfb\u53d6JSON\u6587\u4ef6\u65f6\u9047\u5230\u89e3\u6790\u9519\u8bef\uff0c\u53ef\u80fd\u662f\u6587\u4ef6\u635f\u574f\u6216\u683c\u5f0f\u4e0d\u6b63\u786e\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>try:<\/p>\n<p>    with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>        data = json.load(file)<\/p>\n<p>except json.JSONDecodeError as e:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u683c\u5f0f\u9519\u8bef:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u63d0\u4f9b\u66f4\u597d\u7684\u9519\u8bef\u4fe1\u606f\u5e76\u91c7\u53d6\u76f8\u5e94\u63aa\u65bd\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u516d\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>JSON\u6587\u4ef6\u5728\u6570\u636e\u5b58\u50a8\u4e0e\u4ea4\u6362\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\uff0c\u638c\u63e1\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6JSON\u6587\u4ef6\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684json\u6a21\u5757\u6216\u7b2c\u4e09\u65b9\u5e93\u5982pandas\u548csimplejson\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8bfb\u53d6\u548c\u5904\u7406JSON\u6570\u636e\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21JSON\u6587\u4ef6\u65f6\uff0c\u5e94\u8003\u8651\u9010\u884c\u89e3\u6790\u6216\u5206\u5757\u8bfb\u53d6\u7684\u65b9\u6cd5\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f00\u53d1\u8005\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u7f16\u7801\u9519\u8bef\u548c\u6587\u4ef6\u635f\u574f\u95ee\u9898\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\u4e0e\u4f18\u5316\uff0c\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684JSON\u6570\u636e\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\u8bfb\u53d6JSON\u6587\u4ef6\u901a\u5e38\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684JSON\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6587\u4ef6\u4e2d\u7684JSON\u6570\u636e\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\nwith open(&#39;file.json&#39;, &#39;r&#39;) as file:\n    data = json.load(file)\n    print(data)\n<\/code><\/pre>\n<p><strong>\u8bfb\u53d6JSON\u6587\u4ef6\u65f6\u9047\u5230\u9519\u8bef\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6JSON\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\uff0c\u6bd4\u5982\u6587\u4ef6\u672a\u627e\u5230\u6216JSON\u683c\u5f0f\u4e0d\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u6349\u8fd9\u4e9b\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u548cJSON\u89e3\u6790\u7684\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\ntry:\n    with open(&#39;file.json&#39;, &#39;r&#39;) as file:\n        data = json.load(file)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u3002&quot;)\nexcept json.JSONDecodeError:\n    print(&quot;JSON\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\u3002&quot;)\n<\/code><\/pre>\n<p><strong>JSON\u6587\u4ef6\u7684\u7ed3\u6784\u662f\u600e\u6837\u7684\uff1f<\/strong><br \/>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\u3002\u5b83\u7684\u57fa\u672c\u7ed3\u6784\u5305\u62ec\u5bf9\u8c61\uff08\u4ee5\u82b1\u62ec\u53f7 <code>{}<\/code> \u8868\u793a\uff09\u548c\u6570\u7ec4\uff08\u4ee5\u65b9\u62ec\u53f7 <code>[]<\/code> \u8868\u793a\uff09\u3002\u5bf9\u8c61\u7531\u952e\u503c\u5bf9\u7ec4\u6210\uff0c\u6570\u7ec4\u5219\u662f\u503c\u7684\u6709\u5e8f\u96c6\u5408\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684JSON\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-json\">{\n    &quot;name&quot;: &quot;Alice&quot;,\n    &quot;age&quot;: 30,\n    &quot;is_student&quot;: false,\n    &quot;courses&quot;: [&quot;Math&quot;, &quot;Science&quot;]\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u5305\u542b\u4e00\u4e2a\u5bf9\u8c61\uff0c\u91cc\u9762\u6709\u591a\u4e2a\u5c5e\u6027\u548c\u4e00\u4e2a\u6570\u7ec4\u3002\u4e86\u89e3JSON\u7684\u7ed3\u6784\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u8fdb\u884c\u6570\u636e\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6JSON\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684json\u6a21\u5757\u3001\u7528pandas\u5e93\u8bfb\u53d6JSON\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":962067,"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\/962063"}],"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=962063"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962063\/revisions"}],"predecessor-version":[{"id":962068,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962063\/revisions\/962068"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962067"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}