首页 > 代码库 > ue4 C++ json数据的读写
ue4 C++ json数据的读写
这是改变恢复机制的json文件的例子
//写入
TSharedPtr<FJsonObject> RootObject = MakeShareable(newFJsonObject);
TArray< TSharedPtr<FJsonValue> > PackagesThatCanBeRestored;
TSharedPtr<FJsonObject> EntryObject = MakeShareable(new FJsonObject);
EntryObject->SetStringField(TEXT("PackagePathName"), TEXT("Temp/Untitled_1"));
EntryObject->SetStringField(TEXT("AutoSavePath"), TEXT("Temp/Untitled_1_Auto1.umap"));
TSharedPtr<FJsonValue> EntryValue = MakeShareable(new FJsonValueObject(EntryObject));
PackagesThatCanBeRestored.Add(EntryValue);
RootObject->SetBoolField(TEXT("RestoreEnabled"), true);
RootObject->SetArrayField(TEXT("Packages"), PackagesThatCanBeRestored);
constFStringFilename = FPaths::GameSavedDir() /TEXT("Autosaves") /TEXT("PackageRestoreData.json");
FArchive* constFileAr = IFileManager::Get().CreateFileWriter(*Filename, FILEWRITE_EvenIfReadOnly);
if (FileAr)
{
TSharedRef<TJsonWriter< TCHAR, TPrettyJsonPrintPolicy<TCHAR> >> Writer = TJsonWriterFactory< TCHAR, TPrettyJsonPrintPolicy<TCHAR> >::Create(FileAr);
FJsonSerializer::Serialize(RootObject.ToSharedRef(), Writer);
FileAr->Close();
}
// 读取本地json文件
const FString Filename = FPaths::GameSavedDir() / TEXT("Autosaves") / TEXT("PackageRestoreData.json");
FArchive* const FileAr = IFileManager::Get().CreateFileReader(*Filename);
bool bJsonLoaded = false;
TSharedPtr<FJsonObject> RootObject = MakeShareable(new FJsonObject);
{
TSharedRef<TJsonReader<TCHAR>> Reader = TJsonReaderFactory<TCHAR>::Create(FileAr);
bJsonLoaded = FJsonSerializer::Deserialize(Reader, RootObject);
FileAr->Close();
}
TMap<FString, FString> PackagesThatCanBeRestored;
TArray< TSharedPtr<FJsonValue> > PackagesThatCanBeRestoredArray = RootObject->GetArrayField(TEXT("Packages"));
int32 TiRen = RootObject->GetIntegerField(TEXT("TiRen"));
for (auto It = PackagesThatCanBeRestoredArray.CreateConstIterator(); It; ++It)
{
TSharedPtr<FJsonObject> EntryObject = (*It)->AsObject();
const FString PackagePathName = EntryObject->GetStringField(TEXT("PackagePathName"));
const FString AutoSavePath = EntryObject->GetStringField(TEXT("AutoSavePath"));
PackagesThatCanBeRestored.Add(PackagePathName, AutoSavePath);
}
for (auto It = PackagesThatCanBeRestored.CreateConstIterator(); It; ++It)
{
if (TiRen == 1)
{
EditorStartupMap = FPaths::GameContentDir() / TEXT("Autosaves") / It->Value;
}
else
{
EditorStartupMap = AutoSaveUtils::GetAutoSaveDir() / It->Value;
}
}
ue4 C++ json数据的读写
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。