UE4 操作JSON

//一段read的操作实例
void UGameInstBase::JsonDeserializeTest()
{
	FString JsonValue = "{\"total\":\"123\", \"intt\":12}";// "[{\"author\":\"4446545646544\"},{\"name\":\"jack\"}]";

	FString JsonStr;
	//TArray<TSharedPtr<FJsonValue>> JsonParsed;
	TSharedPtr<FJsonValue> JsonParsed;
	const TSharedRef< TJsonReader<TCHAR> > JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonValue);
	
	bool BFlag = FJsonSerializer::Deserialize(JsonReader, JsonParsed);
	if (BFlag)
	{
		if (GEngine)
		{
			FString TmpString = "true0000000000";
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TmpString);
		}

		FString FStringAuthor = JsonParsed->AsObject()->GetStringField("total");
		if (GEngine)
		{
			FString TmpString = "total :" + FStringAuthor;
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TmpString);
		}
		int FIntt = JsonParsed->AsObject()->GetIntegerField("intt");
		if (GEngine)
		{
			FString TmpString = "intt :";
			TmpString.AppendInt(FIntt);
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TmpString);
		}
		/*int ArrayNumber = JsonParsed.Num();
		if (ArrayNumber == 2)
		{
			FString FStringAuthor = JsonParsed[0]->AsObject()->GetStringField("author");
			if (GEngine)
			{
				FString TmpString = "author :" + FStringAuthor;
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TmpString);
			}
			FString FStringName = JsonParsed[1]->AsObject()->GetStringField("name");
			if (GEngine)
			{
				FString TmpString = "name :" + FStringName;
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TmpString);
			}
		}*/
	}
	else 
	{
		if (GEngine)
		{
			FString TmpString = "false0000000000";
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TmpString);
		}
	}

}

https://blog.csdn.net/qq_35760525/article/details/77531286

https://api.unrealengine.com/INT/API/Runtime/Json/Serialization/FJsonSerializer/index.html

 

!!!很重要的一点是,需要再ue4工程的duild.cs中加入Json模块,eg.

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore",  "Json" });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值