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"));
	}
}
相关推荐
九河云2 小时前
不同级别华为云代理商的增值服务内容与质量差异分析
大数据·服务器·人工智能·科技·华为云
SongYuLong的博客3 小时前
Ubuntu24.04搭建GitLab服务器
运维·服务器·gitlab
leonardee3 小时前
Spring Security安全框架原理与实战
java·后端
q***5183 小时前
Spring Cloud gateway 路由规则
java
tzhou644523 小时前
Linux文本处理工具:cut、sort、uniq、tr
linux·运维·服务器
空空kkk4 小时前
SpringMVC框架——入门
java·spring
liyi_hz20084 小时前
云原生 + 国产化适配:O2OA (翱途)开发平台后端技术栈深度解析
java·后端·开源软件
⑩-4 小时前
缓存穿透,击穿,雪崩
java·redis
合作小小程序员小小店4 小时前
web网页开发,在线%考试管理%系统,基于Idea,html,css,jQuery,java,jsp,servlet,mysql。
java·前端·intellij-idea
顾安r4 小时前
11.19 脚本 最小web控制linux/termux
linux·服务器·css·flask