Json导入导出DataTable
Json文件夹导入DataTable
DataTable->EmptyTable();
TArray<FString> JsonFiles;
IFileManager::Get().FindFilesRecursive(JsonFiles, *JsonFolderPath, TEXT("*.json"), true, false, true);
for(const auto JsonFilePath : JsonFiles)
{
FString JsonFileString;
FFileHelper::LoadFileToString(JsonFileString, *JsonFilePath);
const TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(JsonFileString);
TSharedPtr<FJsonObject> JsonObject;
FJsonSerializer::Deserialize(JsonReader, JsonObject);
if(JsonObject.IsValid() == false)
{
UE_LOG(DataTableDynamicSync, Error, TEXT("Failed to read file %s, please check if the Json object is correct"), *JsonFilePath);
continue;
}
void* RowData = FMemory::Malloc(DataTable->GetRowStruct()->GetStructureSize());
FJsonObjectConverter::JsonObjectToUStruct(JsonObject.ToSharedRef(), DataTable->GetRowStruct(), RowData);
DataTable->AddRow(FName(JsonFilePath.RightChop(JsonFolderPath.Len()).LeftChop(5)), *static_cast<FTableRowBase*>(RowData));
}
#if WITH_EDITOR
DataTable->Modify(true);
#endif
return true;
这是完全规避编辑器的写法,可以正常打包使用
DataTable导出到文件夹
IFileManager::Get().DeleteDirectory(*JsonFolderPath, true, true);
for(auto const& It : DataTable->GetRowMap())
{
FString JsonFileString;
FJsonObjectConverter::UStructToJsonObjectString(DataTable->GetRowStruct(), It.Value, JsonFileString);
FFileHelper::SaveStringToFile(JsonFileString, *(JsonFolderPath + It.Key.ToString() + TEXT(".json")));
}
return true
同样是完全规避了编辑器,可以正常打包使用