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"));
	}
}
相关推荐
OsDepK15 小时前
OSMDE_v1.9.0已加入FTP与shell功能
linux·运维·服务器
yuniko-n15 小时前
【Java】字符串相关坑点
java·开发语言·python
Terra.K15 小时前
Spring AI day1(SSE+AI)
java·人工智能·spring·springai
斑鸠喳喳15 小时前
线程本地存储 ThreadLocal
java·后端
剑锋所指,所向披靡!15 小时前
HTTP服务器的基本相关概念
服务器·网络协议·http
Wang's Blog15 小时前
Java框架 SpringCloud 快速入门: 服务拆分案例 Demo
java·开发语言·spring cloud
硅基手札15 小时前
【Linux内核专栏 11】驱动框架:platform / char / misc / 设备树
linux·运维·服务器
liangshanbo121516 小时前
高级前端面试题:package.json 常见字段
前端·json
2301_7779983416 小时前
TCP协议:滑动窗口与流量控制
服务器·网络·网络协议·tcp/ip·php
DongQiShanRen16 小时前
裁决台账双向互校(上):名册与实物的第一道对账
java·linux·运维·数据库·人工智能·自然语言处理·数据挖掘