JSON Reader

一.大体思路

承接JSON Writer-CSDN博客文章

1.首先 我们同样通过 LoadFileToString 将文件 读到FString里。这里是GoodJsonString

2.声明出来一个FJsonObject,

TJsonReader<TCHAR> 是 通过TJ送Reader Factory<TCHAR>里的Ceate 制造出来,并且在Create时,里面传入之前读到的FString。

3.这个时候,通过将FJsonSerializer类里的Deserialize函数,传入

(TJsonReader<TCHAR>,FJsonObject)

这个时候,就实现了,FString到FJsonReader,再到FJsonObject。

4.JsonObject又有各种As开头的函数,进行转换为对应的变量。

cpp 复制代码
void ASimpleActor::ReadJSON()
{
	FString GoodJsonString = TEXT("");
	FString FilePath = FPaths::ProjectSavedDir() / TEXT("GoodJSON.json");
	//序列化
	FFileHelper::LoadFileToString(GoodJsonString, *FilePath);
	//TSharedRef<TJsonReader<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>> GoodJsonWriter =
	//	TJsonReader<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>::Create(&GoodJsonString);

	TSharedPtr<FJsonObject> BackGoodJSON;
	TSharedRef<TJsonReader<TCHAR>> BackGoodJSONReader =
	TJsonReaderFactory<TCHAR>::Create(GoodJsonString);

	bool IsDeseriablize = FJsonSerializer::Deserialize(BackGoodJSONReader,BackGoodJSON);   //需要Json模块
	if (IsDeseriablize)
	{
		TSharedPtr<FJsonValue> CodeJsonValue = BackGoodJSON->TryGetField(TEXT("Code"));
		if (CodeJsonValue.IsValid())
		{
			//GoodJsonString += CodeJsonValue->AsNumber();
			int32 CodeNum1 = CodeJsonValue->AsNumber();
		}
		TSharedPtr<FJsonValue> MessageJsonValue = BackGoodJSON->TryGetField(TEXT("Message"));
		if (CodeJsonValue.IsValid())
		{
			FString MessageString = MessageJsonValue->AsString();
		}
		TSharedPtr<FJsonValue>  InsideJsonValue = BackGoodJSON->TryGetField(TEXT("WX"));
		if (InsideJsonValue.IsValid())
		{
			TSharedPtr<FJsonObject> JsonObject = InsideJsonValue->AsObject();
			if (JsonObject.IsValid())
			{
				TSharedPtr<FJsonValue> tmpValue = JsonObject->TryGetField(TEXT("ServerVersion"));
				if (tmpValue.IsValid())
				{
					FString Value = tmpValue->AsString();
					UE_LOG(LogJson, Error, TEXT("%s"), *Value);
				}
			}
		}
	}

}

二.FJsonObject的解析

在JsonObject时有三种方式,解析的函数。

cpp 复制代码
void ASimpleActor::ReadJSON2()
{
	FString GoodJsonString = TEXT("");
	FString FilePath = FPaths::ProjectSavedDir() / TEXT("GoodJSON.json");
	//反序列化
	FFileHelper::LoadFileToString(GoodJsonString, *FilePath);
	TSharedPtr<FJsonObject> BackGoodJSONObject;
	TSharedRef<TJsonReader<TCHAR>> BackGoodJSONReader =
		TJsonReaderFactory<TCHAR>::Create(GoodJsonString);

	bool IsDesirialize = FJsonSerializer::Deserialize(BackGoodJSONReader, BackGoodJSONObject);  
	//反序列化,将GoodJsonString 流 反序列化 到 用JsoonReader 反序列化到 JsonObject 上
	if (IsDesirialize)
	{
		TSharedPtr<FJsonValue> CodeJsonValue = BackGoodJSONObject->TryGetField(TEXT("Code"));  //返回Ptr,what Fuck
		if (CodeJsonValue.IsValid())
		{
			int32 CodeNum1 = CodeJsonValue->AsNumber();
		}
		int32 CodeNum2 = -1;
		//第二种方式
		bool bFindCode = BackGoodJSONObject->TryGetNumberField(TEXT("Code"),CodeNum2);
		//第三种方式
		int32 CodeNum3 = BackGoodJSONObject->GetNumberField(TEXT("Code"));
	}
}
相关推荐
karry_k11 小时前
MyBatis批量insert-select踩坑:useGeneratedKeys=true 可能让PostgreSQL返回大量插入结果
java·后端
karry_k12 小时前
PostgreSQL 在 MyBatis 中执行正常 SQL 失效:一次 DELETE USING 踩坑记录
java·后端
SamDeepThinking15 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
她的男孩18 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
你好潘先生19 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
荣码20 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
plainGeekDev21 小时前
Gson → kotlinx.serialization
android·java·kotlin
小bo波1 天前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing