UE Json读写

1:添加JSON Modules

在Project.Build.cs文件中的PrivateDependencyModuleNames添加Json和JsonUtilities模块。

读取Json文件到字符串

privatebool LoadStringFromFile(const FString& FileName, const FString& RelaPath, FString& ResultString);

bool SlAiJsonHandle::LoadStringFromFile(const FString& FileName, const FString& RelaPath, FString& ResultString)
{
   
	if (!FileName.IsEmpty()) {
   
		//获取绝对路径 (GameContent+相对路径+文件名)
		FString AbsoPath = FPaths::GameContentDir() + RelaPath + FileName;
		//判断文件是否存在文件
		if (FPaths::FileExists(AbsoPath)) 
		{
   
			if (FFileHelper::LoadFileToString(ResultString, *AbsoPath))
			{
   
				return true;
			}
			else {
   
				//加载不成功
				SlAiHelper::Debug(FString("Load Error") + AbsoPath);
			}
		}
		else {
   
			//输出文件不存在
			SlAiHelper::Debug(FString("File Not Exist") + AbsoPath);
		}
	}
	return false;
}

解析存档方法 (读取到的json文件按内容,传入方法)

void RecordDataJsonRead(FString& Culture, float& MusicVolume, float& SoundVolume, TArray<FString>& RecordDataList);


void SlAiJsonHandle::RecordDataJsonRead(FString& Culture, float& MusicVolume, float& SoundVolume, TArray<FString>& RecordDataList)
{
   
	//用于保存file读取的内容
	FString JsonValue;
	//载入
	LoadStringFromFile(RecordDataFileName, RelativePath, JsonValue);
	
	//保存解析出来的数据
	TArray<TSharedPtr<FJsonValue>> JsonParsed;  
	//将JsonValue字符串中读取到JsonReader
	TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonValue);
	
	
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值