c++ ue 读写json
时间: 2024-12-27 13:20:54 浏览: 67
在C++中使用Unreal Engine (UE) 进行JSON文件的读写,通常会利用第三方库,比如JsonCpp、TinyJSON、nlohmann/json等。以下是使用JsonCpp库的一个简单示例:
```cpp
// 引入必要的库
#include "CoreMinimal.h"
#include "JsonObject.h"
// JsonCpp库
#include "json/json.h"
// 读取JSON文件
FString ReadJsonFromFile(const FString& FilePath) {
FString FileContents;
FFileHelper::LoadFileToString(FileContents, *FilePath);
// 解析JSON字符串
const nlohmann::json& jsonData = nlohmann::parse(FileContents);
return jsonData.dump(); // 返回解析后的字符串形式的JSON
}
// 写入JSON文件
void WriteJsonToFile(const nlohmann::json& Data, const FString& FilePath) {
TSharedRef<FJsonObject> json = MakeShareable(new FJsonObject);
json->Set(TCHAR_TO_UTF8(*Data.dump())); // 将JSON数据转换为FJsonObject
FFileHelper::SaveStringToFile(*json->ToJson(), *FilePath);
}
阅读全文
相关推荐


















