{"id":205246,"date":"2025-05-29T09:04:12","date_gmt":"2025-05-29T01:04:12","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205246\/"},"modified":"2025-05-29T09:04:12","modified_gmt":"2025-05-29T01:04:12","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e4%bb%a3%e7%a0%81%e4%bf%ae%e6%94%b9-json-%e6%96%87%e4%bb%b6%e7%9a%84%e6%8c%87%e5%ae%9a%e5%ad%97%e6%ae%b5%ef%bc%8c%e5%b9%b6%e5%b0%86%e5%85%b6%e4%b8%8e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205246\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u7684\u6307\u5b9a\u5b57\u6bb5\uff0c\u5e76\u5c06\u5176\u4e0e\u6587\u4ef6\u5939\u5185\u7684\u5176\u4ed6\u6587\u4ef6\u590d\u5236\u5230\u65b0\u7684\u8def\u5f84\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u7684\u6307\u5b9a\u5b57\u6bb5\uff0c\u5e76\u5c06\u5176\u4e0e\u6587\u4ef6\u5939\u5185\u7684\u5176\u4ed6\u6587\u4ef6\u590d\u5236\u5230\u65b0\u7684\u8def\u5f84\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u7684\u6307\u5b9a\u5b57\u6bb5\uff0c\u5e76\u5c06\u5176\u4e0e\u6587\u4ef6\u5939\u5185\u7684\u5176\u4ed6\u6587\u4ef6\u590d\u5236\u5230\u65b0\u7684\u8def\u5f84\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/173267715267468e201ce66.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4fee\u6539 json \u6587\u4ef6\u5e76\u521b\u5efa\u65b0\u6587\u4ef6\u5939\u62f7\u8d1d\u81f3\u6307\u5b9a\u8def\u5f84<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u6c42\u52a9\u4e8e python \u4ee3\u7801\u6765\u534f\u52a9\u5b8c\u6210\u4ee5\u4e0b\u4efb\u52a1\uff1a<\/p>\n<ul>\n<li>\u4fee\u6539\u6587\u4ef6\u5939\u5185 json \u6587\u4ef6\u7684\u6307\u5b9a\u5b57\u6bb5\u3002<\/li>\n<li>\u5c06\u4fee\u6539\u540e\u7684 json \u6587\u4ef6\u548c\u6587\u4ef6\u5939\u5185\u5176\u4ed6\u6587\u4ef6\uff08\u5982 jpg \u548c avi\uff09\u62f7\u8d1d\u5230\u4e00\u4e2a\u65b0\u7684\u8def\u5f84\u4e0b\u3002<\/li>\n<li>\u5982\u679c\u65b0\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u521b\u5efa\u65b0\u6587\u4ef6\u5939\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import os\nimport json\nimport shutil\n\ndef get_json_data(old_json_path):\n    result = []\n    for root, dirs, files in os.walk(old_json_path):\n        for file_name in files:\n            if file_name.endswith('.json'):\n                old_json_file_path = os.path.join(root, file_name)\n                result.append(old_json_file_path)\n    return result\n\ndef get_dir_name(dir_name):\n    dir_name_result = []\n    for root, dirs, files in os.walk(dir_name):\n        for dir in dirs:\n            dirpath = os.path.join(root, dir)\n            dir_name_result.append(dirpath)\n    return dir_name_result\n\ndef read_json(filepath):\n    with open(filepath, 'r', encoding='utf-8') as f:\n        json_data = f.read()\n        json_data_str = eval(json_data)\n        json_data_str.update({\"fileName\": filepath.split(\"\\\\\")[-2].replace(\".dav\", \".avi\")})\n    return json_data_str\n\ndef write_json(filepath, newdata):\n    with open(filepath, 'w', encoding='utf-8') as r:\n        json.dump(newdata, r, indent=4, ensure_ascii=False)\n\ndef copy_other_files(old_json_path, new_json_path):\n    for root, dirs, files in os.walk(old_json_path):\n        for file_name in files:\n            if not file_name.endswith('.json'):\n                old_json_filepath = os.path.join(root, file_name)\n                new_dir_name = os.path.join(new_json_path, root.split(\"\\\\\")[-1])\n                if not os.path.exists(new_dir_name):\n                    os.mkdir(new_dir_name)\n                shutil.copy(old_json_filepath, new_dir_name)\n\nif __name__ == \"__main__\":\n    old_json_path = r'D:\\ss'\n    new_json_path = r'D:\\json'\n    dirlist = get_dir_name(old_json_path)\n    for dir in dirlist:\n        json_file_list = get_json_data(dir)\n        for json_file in json_file_list:\n            dir_name = dir.split(\"\\\\\")[-1].replace(\".dav\", \".avi\")\n            new_dir_name = os.path.join(new_json_path, dir_name)\n            if not os.path.exists(new_dir_name):\n                os.mkdir(new_dir_name)\n            shutil.copy(json_file, new_dir_name)\n            newdata = read_json(new_dir_name + \"\\\\\" + os.path.basename(json_file))\n            write_json(new_dir_name + \"\\\\\" + os.path.basename(json_file), newdata)\n        copy_other_files(dir, new_json_path)<\/pre>\n<p><strong>\u89e3\u51b3\u95ee\u9898\uff1a<\/strong><\/p>\n<p><strong>\u95ee\u9898\u4e00\uff1a<\/strong><\/p>\n<p>\u786e\u4fdd get_json_data \u51fd\u6570\u6b63\u786e\u8fc7\u6ee4\u4e0d\u9700\u8981\u590d\u5236\u7684\u6587\u4ef6\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u4ec5\u62f7\u8d1d json \u6587\u4ef6\u3002<\/p>\n<p><strong>\u95ee\u9898\u4e8c\uff1a<\/strong><\/p>\n<p>\u5728\u8bfb\u53d6\u7a7a\u5b57\u7b26\u4e32\u65f6\uff0ceval \u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u5c06 bool(data) \u68c0\u67e5\u6dfb\u52a0\u5230 eval \u4e4b\u524d\uff0c\u4ee5\u907f\u514d\u7a7a\u5b57\u7b26\u4e32\u9519\u8bef\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u7684\u6307\u5b9a\u5b57\u6bb5\uff0c\u5e76\u5c06\u5176\u4e0e\u6587\u4ef6\u5939\u5185\u7684\u5176\u4ed6\u6587\u4ef6\u590d\u5236\u5230\u65b0\u7684\u8def\u5f84\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205246","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205246"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205246\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}