//一段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" });