json文件合并

实现将test.json的list和test_write.json的list合并,并且写入到test_okok.json

此类方法可用于文件中 这里是引用 没有数字 的情况
test.json文件内容如下:

  [
      'qwe',
      'asd',
      'zxc',
      'qweqweqweqweqweqweqweqwe'
  ]

test_write.json内容如下:

  [
      '123',
      {
          "name": "aaa",
          "age": {
              "addr": "ape-redis-00.bcc-szth.baidu.com",
              "port": 8604
         }
     },
     '789'
 ]

代码实现如下

import os
import json
import ast

fo = open(afs_client.afs_client_dir + '/test.json', "r")
str1 = fo.read()
b = ast.literal_eval(str1)
fo.close()
fo2 = open(afs_client.afs_client_dir + '/test_write.json', "r")
str2 = fo2.read()
b2 = ast.literal_eval(str2)
fo2.close()
b.extend(b2)
fo3 = open(afs_client.afs_client_dir + '/test_okok.json', "w")
json.dump(b, fo3, indent=4)
fo3.close()

test_okok.json文件内容如下:

 [
     "qwe",
     "asd",
     "zxc",
     "qweqweqweqweqweqweqweqwe",
     "123",
     {
         "name": "aaa",
         "age": {
             "addr": "ape-redis-00.bcc-szth.baidu.com",
             "port": 8604
         }
     },
     "789"
 ]

### 如何在 Unreal Engine 中合并 JSON 文件 为了实现多个 JSON 文件合并,在 Unreal Engine 的环境中通常会涉及到读取、解析以及写回这些文件的操作。虽然 Unreal Engine 主要用于游戏开发,但其强大的 C++ 和蓝图系统允许开发者处理各种数据格式,包括 JSON。 #### 使用 C++ 合并 JSON 文件 对于更复杂的操作或是性能敏感的应用场景,建议采用 C++ 来完成这项工作: ```cpp #include "Dom/JsonObject.h" #include "Serialization/JsonReader.h" #include "Serialization/JsonSerializer.h" void MergeJSONFiles(const FString& SourceFile1, const FString& SourceFile2, const FString& DestinationFile) { TSharedPtr<FJsonObject> JsonObject1; TSharedPtr<FJsonObject> JsonObject2; // Load and parse the first JSON file into a FJsonObject pointer. { TArray<uint8> FileData; if (FFileHelper::LoadFileToArray(FileData, *SourceFile1)) { TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create((const TCHAR*)FileData.GetData()); if (!FJsonSerializer::Deserialize(Reader, JsonObject1) || !JsonObject1.IsValid()) UE_LOG(LogTemp, Error, TEXT("Failed to load %s"), *SourceFile1); } } // Similarly for second json object... { TArray<uint8> FileData; if (FFileHelper::LoadFileToArray(FileData, *SourceFile2)) { TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create((const TCHAR*)FileData.GetData()); if (!FJsonSerializer::Deserialize(Reader, JsonObject2) || !JsonObject2.IsValid()) UE_LOG(LogTemp, Error, TEXT("Failed to load %s"), *SourceFile2); } } // Assuming both files contain objects at their root level, // we can now merge them by iterating over keys from one of the source objects. for (TMap<FString, TSharedPtr<FJsonValue>>::TIterator It(JsonObject2->Values); It; ++It) { JsonObject1->SetField(It.Key(), It.Value()); } // Serialize merged data back out as string or directly write it to disk using FFileHelper functions. } ``` 这段代码展示了如何加载两个 JSON 文件的内容至 `FJsonObject` 对象中,并将第二个对象的所有键值对复制到第一个对象里[^1]。最后一步则是将修改后的 JSON 数据序列化成字符串形式或直接保存到磁盘上指定位置。 请注意上述方法假设所有的顶层元素都是字典(即键值对集合),如果遇到数组或其他结构,则需调整相应的逻辑来适配具体需求。 #### 利用 Python Scripting 插件 另一种方式是在虚幻引擎内部利用Python脚本来简化此流程。这需要先确保启用了 Python Plugin 并配置好环境变量以便支持外部库如 `json` 或者其他第三方包来进行高效的数据处理[^3]。 ```python import os import json def merge_json_files(file_path_1, file_path_2, output_file): with open(file_path_1, 'r') as f: dict1 = json.load(f) with open(file_path_2, 'r') as f: dict2 = json.load(f) combined_dict = {**dict1, **dict2} with open(output_file, 'w') as outfile: json.dump(combined_dict, outfile, indent=4) merge_json_files('C:/path/to/file1.json', 'C:/path/to/file2.json', 'C:/path/to/outputfile.json') ``` 这种方法更加直观易懂,适合那些熟悉 Python 编程语言的人士使用。它同样能够有效地解决大多数情况下简单的 JSON 文件合并问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值