{"id":1120837,"date":"2025-01-08T19:02:11","date_gmt":"2025-01-08T11:02:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120837.html"},"modified":"2025-01-08T19:02:33","modified_gmt":"2025-01-08T11:02:33","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e4%b8%a4%e4%b8%aajson%e6%96%87%e4%bb%b6%e5%90%88%e5%b9%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120837.html","title":{"rendered":"python\u5982\u4f55\u628a\u4e24\u4e2ajson\u6587\u4ef6\u5408\u5e76"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083334\/7d1eafd1-3cd8-4fb0-97bb-f2dbcf3ba74f.webp\" alt=\"python\u5982\u4f55\u628a\u4e24\u4e2ajson\u6587\u4ef6\u5408\u5e76\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u4e24\u4e2aJSON\u6587\u4ef6\u5408\u5e76\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u76f4\u63a5\u52a0\u8f7d\u5e76\u5408\u5e76\u5b57\u5178\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u52a0\u8f7dJSON\u6587\u4ef6\u3001\u5408\u5e76\u5b57\u5178\u3001\u4fdd\u5b58\u5408\u5e76\u540e\u7684\u7ed3\u679c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u7684\u6838\u5fc3\u5728\u4e8e\uff1a\u52a0\u8f7d\u3001\u5408\u5e76\u3001\u4fdd\u5b58\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u52a0\u8f7d<\/strong>\u662f\u6307\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u5e76\u89e3\u6790\u4e3aPython\u5b57\u5178\uff0c<strong>\u5408\u5e76<\/strong>\u662f\u6307\u5c06\u4e24\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c<strong>\u4fdd\u5b58<\/strong>\u662f\u6307\u5c06\u5408\u5e76\u540e\u7684\u5b57\u5178\u91cd\u65b0\u4fdd\u5b58\u4e3aJSON\u6587\u4ef6\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u52a0\u8f7dJSON\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u8bfb\u53d6JSON\u6587\u4ef6\u3002<code>json<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>load<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06JSON\u6587\u4ef6\u5185\u5bb9\u89e3\u6790\u4e3aPython\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u52a0\u8f7d\u7b2c\u4e00\u4e2aJSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;file1.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>    json_data1 = json.load(f)<\/p>\n<h2><strong>\u52a0\u8f7d\u7b2c\u4e8c\u4e2aJSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;file2.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>    json_data2 = json.load(f)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5408\u5e76\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u5408\u5e76\u5b57\u5178\u662f\u5c06\u4e24\u4e2a\u5b57\u5178\u7684\u6570\u636e\u5408\u5e76\u5230\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u8fd9\u91cc\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u5b57\u5178\u7684 <code>update<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684 <code>update<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u5982\u679c\u4e24\u4e2a\u5b57\u5178\u6709\u76f8\u540c\u7684\u952e\uff0c\u540e\u8005\u7684\u503c\u4f1a\u8986\u76d6\u524d\u8005\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528update\u65b9\u6cd5\u5408\u5e76\u5b57\u5178<\/p>\n<p>merged_data = json_data1.copy()  # \u590d\u5236\u7b2c\u4e00\u4e2a\u5b57\u5178\uff0c\u4ee5\u514d\u6539\u53d8\u539f\u59cb\u6570\u636e<\/p>\n<p>merged_data.update(json_data2)   # \u66f4\u65b0\u7b2c\u4e00\u4e2a\u5b57\u5178<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u5b57\u5178\u89e3\u5305\uff08Python 3.5+\uff09<\/h3>\n<\/p>\n<p><p>\u5728Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u89e3\u5305\u7684\u65b9\u5f0f\u6765\u5408\u5e76\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u89e3\u5305\u5408\u5e76\u5b57\u5178<\/p>\n<p>merged_data = {&lt;strong&gt;json_data1, &lt;\/strong&gt;json_data2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5904\u7406\u5d4c\u5957\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5982\u679cJSON\u6587\u4ef6\u5305\u542b\u5d4c\u5957\u5b57\u5178\uff0c\u6211\u4eec\u9700\u8981\u9012\u5f52\u5730\u5408\u5e76\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u73b0\u9012\u5f52\u5408\u5e76\u5b57\u5178\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_dicts(dict1, dict2):<\/p>\n<p>    result = dict1.copy()<\/p>\n<p>    for key, value in dict2.items():<\/p>\n<p>        if key in result and isinstance(result[key], dict) and isinstance(value, dict):<\/p>\n<p>            result[key] = merge_dicts(result[key], value)<\/p>\n<p>        else:<\/p>\n<p>            result[key] = value<\/p>\n<p>    return result<\/p>\n<h2><strong>\u9012\u5f52\u5408\u5e76\u5d4c\u5957\u5b57\u5178<\/strong><\/h2>\n<p>merged_data = merge_dicts(json_data1, json_data2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4fdd\u5b58\u5408\u5e76\u540e\u7684\u7ed3\u679c<\/h2>\n<\/p>\n<p><p>\u5c06\u5408\u5e76\u540e\u7684\u5b57\u5178\u4fdd\u5b58\u4e3aJSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u7684<code>dump<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u5408\u5e76\u540e\u7684JSON\u6587\u4ef6<\/p>\n<p>with open(&#39;merged_file.json&#39;, &#39;w&#39;) as f:<\/p>\n<p>    json.dump(merged_data, f, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\uff0cJSON\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5d4c\u5957\u7684\u5bf9\u8c61\u7b49\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1. \u5408\u5e76\u5305\u542b\u5217\u8868\u7684JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e24\u4e2aJSON\u6587\u4ef6\u7684\u67d0\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\u662f\u5217\u8868\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_dicts_with_lists(dict1, dict2):<\/p>\n<p>    result = dict1.copy()<\/p>\n<p>    for key, value in dict2.items():<\/p>\n<p>        if key in result and isinstance(result[key], dict) and isinstance(value, dict):<\/p>\n<p>            result[key] = merge_dicts_with_lists(result[key], value)<\/p>\n<p>        elif key in result and isinstance(result[key], list) and isinstance(value, list):<\/p>\n<p>            result[key].extend(value)<\/p>\n<p>        else:<\/p>\n<p>            result[key] = value<\/p>\n<p>    return result<\/p>\n<h2><strong>\u5408\u5e76\u5305\u542b\u5217\u8868\u7684JSON\u6587\u4ef6<\/strong><\/h2>\n<p>merged_data = merge_dicts_with_lists(json_data1, json_data2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5904\u7406\u51b2\u7a81<\/h3>\n<\/p>\n<p><p>\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u5178\u5305\u542b\u76f8\u540c\u7684\u952e\u4f46\u5bf9\u5e94\u7684\u503c\u4e0d\u540c\uff0c\u6211\u4eec\u9700\u8981\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u9009\u62e9\u4fdd\u7559\u7b2c\u4e00\u4e2a\u5b57\u5178\u7684\u503c\u3001\u4fdd\u7559\u7b2c\u4e8c\u4e2a\u5b57\u5178\u7684\u503c\uff0c\u6216\u8005\u6839\u636e\u67d0\u4e9b\u89c4\u5219\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_dicts_with_conflicts(dict1, dict2, conflict_resolver):<\/p>\n<p>    result = dict1.copy()<\/p>\n<p>    for key, value in dict2.items():<\/p>\n<p>        if key in result and isinstance(result[key], dict) and isinstance(value, dict):<\/p>\n<p>            result[key] = merge_dicts_with_conflicts(result[key], value, conflict_resolver)<\/p>\n<p>        elif key in result and result[key] != value:<\/p>\n<p>            result[key] = conflict_resolver(result[key], value)<\/p>\n<p>        else:<\/p>\n<p>            result[key] = value<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b\u51b2\u7a81\u89e3\u51b3\u51fd\u6570\uff1a\u4fdd\u7559\u8f83\u5927\u7684\u503c<\/strong><\/h2>\n<p>def conflict_resolver(value1, value2):<\/p>\n<p>    return value1 if value1 &gt; value2 else value2<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51b2\u7a81\u89e3\u51b3\u51fd\u6570\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>merged_data = merge_dicts_with_conflicts(json_data1, json_data2, conflict_resolver)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u64cd\u4f5cJSON\u6570\u636e\u3002\u4f8b\u5982\uff0c<code>jsonmerge<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5408\u5e76JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5<code>jsonmerge<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>jsonmerge<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install jsonmerge<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>jsonmerge<\/code>\u5e93\u5408\u5e76JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>jsonmerge<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\u6765\u5408\u5e76JSON\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>jsonmerge<\/code>\u5e93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jsonmerge import merge<\/p>\n<h2><strong>\u4f7f\u7528jsonmerge\u5e93\u5408\u5e76JSON\u6587\u4ef6<\/strong><\/h2>\n<p>merged_data = merge(json_data1, json_data2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u81ea\u5b9a\u4e49\u5408\u5e76\u7b56\u7565<\/h3>\n<\/p>\n<p><p><code>jsonmerge<\/code>\u5e93\u5141\u8bb8\u6211\u4eec\u81ea\u5b9a\u4e49\u5408\u5e76\u7b56\u7565\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6307\u5b9a\u5982\u4f55\u5904\u7406\u5217\u8868\u3001\u5bf9\u8c61\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jsonmerge import Merger<\/p>\n<h2><strong>\u5b9a\u4e49\u5408\u5e76\u7b56\u7565<\/strong><\/h2>\n<p>schema = {<\/p>\n<p>    &quot;properties&quot;: {<\/p>\n<p>        &quot;list&quot;: {<\/p>\n<p>            &quot;mergeStrategy&quot;: &quot;append&quot;<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaMerger\u5bf9\u8c61<\/strong><\/h2>\n<p>merger = Merger(schema)<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u5408\u5e76\u7b56\u7565\u5408\u5e76JSON\u6587\u4ef6<\/strong><\/h2>\n<p>merged_data = merger.merge(json_data1, json_data2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5b9e\u8df5\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5408\u5e76JSON\u6587\u4ef6\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u548c\u6311\u6218\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<p><h3>1. \u6570\u636e\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u5728\u5408\u5e76JSON\u6587\u4ef6\u4e4b\u524d\uff0c\u6700\u597d\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u548c\u5185\u5bb9\u7b26\u5408\u9884\u671f\u3002\u53ef\u4ee5\u4f7f\u7528<code>jsonschema<\/code>\u5e93\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jsonschema import validate, ValidationError<\/p>\n<h2><strong>\u5b9a\u4e49JSON schema<\/strong><\/h2>\n<p>schema = {<\/p>\n<p>    &quot;type&quot;: &quot;object&quot;,<\/p>\n<p>    &quot;properties&quot;: {<\/p>\n<p>        &quot;name&quot;: {&quot;type&quot;: &quot;string&quot;},<\/p>\n<p>        &quot;age&quot;: {&quot;type&quot;: &quot;integer&quot;},<\/p>\n<p>    },<\/p>\n<p>    &quot;required&quot;: [&quot;name&quot;, &quot;age&quot;]<\/p>\n<p>}<\/p>\n<h2><strong>\u9a8c\u8bc1JSON\u6570\u636e<\/strong><\/h2>\n<p>try:<\/p>\n<p>    validate(instance=json_data1, schema=schema)<\/p>\n<p>    validate(instance=json_data2, schema=schema)<\/p>\n<p>except ValidationError as e:<\/p>\n<p>    print(f&quot;Invalid JSON data: {e.message}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679cJSON\u6587\u4ef6\u975e\u5e38\u5927\uff0c\u76f4\u63a5\u52a0\u8f7d\u548c\u5408\u5e76\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\u95ee\u9898\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u548c\u5904\u7406\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\u5e76\u5408\u5e76<\/strong><\/h2>\n<p>def merge_large_json_files(file1, file2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2, open(output_file, &#39;w&#39;) as out:<\/p>\n<p>        for line1, line2 in zip(f1, f2):<\/p>\n<p>            json_data1 = json.loads(line1)<\/p>\n<p>            json_data2 = json.loads(line2)<\/p>\n<p>            merged_data = merge(json_data1, json_data2)<\/p>\n<p>            json.dump(merged_data, out)<\/p>\n<p>            out.write(&#39;\\n&#39;)<\/p>\n<p>merge_large_json_files(&#39;large_file1.json&#39;, &#39;large_file2.json&#39;, &#39;merged_large_file.json&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5e76\u53d1\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5408\u5e76\u5927\u91cfJSON\u6587\u4ef6\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u53d1\u5904\u7406\u6765\u63d0\u9ad8\u6548\u7387\u3002Python\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u5e76\u53d1\u5904\u7406\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<h2><strong>\u5e76\u53d1\u5408\u5e76JSON\u6587\u4ef6<\/strong><\/h2>\n<p>def merge_json_files(file1, file2):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        json_data1 = json.load(f1)<\/p>\n<p>        json_data2 = json.load(f2)<\/p>\n<p>        merged_data = merge(json_data1, json_data2)<\/p>\n<p>        return merged_data<\/p>\n<p>files_to_merge = [(&#39;file1.json&#39;, &#39;file2.json&#39;), (&#39;file3.json&#39;, &#39;file4.json&#39;)]<\/p>\n<p>with ThreadPoolExecutor() as executor:<\/p>\n<p>    results = list(executor.map(lambda files: merge_json_files(*files), files_to_merge))<\/p>\n<h2><strong>\u4fdd\u5b58\u7ed3\u679c<\/strong><\/h2>\n<p>with open(&#39;merged_files.json&#39;, &#39;w&#39;) as f:<\/p>\n<p>    json.dump(results, f, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5408\u5e76\u4e24\u4e2aJSON\u6587\u4ef6\uff0c\u5e76\u5904\u7406\u5408\u5e76\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u5404\u79cd\u60c5\u51b5\u548c\u6311\u6218\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5b57\u5178\u5408\u5e76\u3001\u5904\u7406\u5d4c\u5957\u7ed3\u6784\u3001\u89e3\u51b3\u51b2\u7a81\uff0c\u8fd8\u662f\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3001\u5904\u7406\u5927\u6587\u4ef6\u548c\u5e76\u53d1\u5408\u5e76\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u64cd\u4f5cJSON\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\u8bfb\u53d6JSON\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\uff0c\u9996\u5148\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00JSON\u6587\u4ef6\uff0c\u63a5\u7740\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u52a0\u8f7d\u4e3aPython\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\nwith open(&#39;file1.json&#39;, &#39;r&#39;) as file:\n    data1 = json.load(file)\n\nwith open(&#39;file2.json&#39;, &#39;r&#39;) as file:\n    data2 = json.load(file)\n<\/code><\/pre>\n<p><strong>\u5408\u5e76\u4e24\u4e2aJSON\u6587\u4ef6\u540e\uff0c\u5982\u4f55\u4fdd\u5b58\u4e3a\u65b0\u7684JSON\u6587\u4ef6\uff1f<\/strong><br \/>\u5408\u5e76\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.dump()<\/code>\u65b9\u6cd5\u5c06\u5408\u5e76\u540e\u7684\u6570\u636e\u4fdd\u5b58\u4e3a\u65b0\u7684JSON\u6587\u4ef6\u3002\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5e76\u6253\u5f00\u5b83\uff0c\u7136\u540e\u5c06\u5408\u5e76\u7684\u6570\u636e\u5199\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;merged.json&#39;, &#39;w&#39;) as file:\n    json.dump(merged_data, file, indent=4)\n<\/code><\/pre>\n<p><strong>\u5408\u5e76JSON\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u91cd\u590d\u7684\u952e\uff1f<\/strong><br \/>\u5728\u5408\u5e76JSON\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u6709\u91cd\u590d\u7684\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.defaultdict<\/code>\u6216\u81ea\u5b9a\u4e49\u903b\u8f91\u6765\u5904\u7406\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u4fdd\u7559\u7b2c\u4e00\u4e2a\u503c\u3001\u6700\u540e\u4e00\u4e2a\u503c\uff0c\u6216\u8005\u5c06\u91cd\u590d\u503c\u5b58\u50a8\u4e3a\u5217\u8868\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import defaultdict\n\nmerged_data = defaultdict(list)\nfor data in [data1, data2]:\n    for key, value in data.items():\n        merged_data[key].append(value)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u91cd\u590d\u7684\u952e\uff0c\u5e76\u5c06\u6570\u636e\u4ee5\u5408\u9002\u7684\u683c\u5f0f\u4fdd\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u4e24\u4e2aJSON\u6587\u4ef6\u5408\u5e76\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u76f4\u63a5\u52a0\u8f7d\u5e76\u5408\u5e76\u5b57\u5178\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u5177\u4f53\u6b65\u9aa4\u5305 [&hellip;]","protected":false},"author":3,"featured_media":1120861,"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\/1120837"}],"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=1120837"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120837\/revisions"}],"predecessor-version":[{"id":1120863,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120837\/revisions\/1120863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120861"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}