I have data endpoint which returns a json like [{"Name": "Alex", "Age": 24}]
now if i Deserialize this as an JsonObject It fails as it's an array with no name how can I get the data inside the array?
Here is an snippet from my code. Should help.
TSharedRef<TJsonReader<>> reader = TJsonReaderFactory<>::Create(data);
TArray< TSharedPtr<FJsonValue> > ue4ObjectArray;
successful = FJsonSerializer::Deserialize(reader, ue4ObjectArray);
TArray<TSharedPtr<FJsonValue>> objArray = JsonObject->GetArrayField(TEXT("AwesomeStructs"));
for (int32 i = 0; i < objArray.Num(); i++)
{
TSharedPtr<FJsonValue> value = objArray[i];
TSharedPtr<FJsonObject> json = value->AsObject();
FVector AwesomeVector = ParseAsVector(json, FString("AwesomeVector"));
bool AwesomeBoolean = json->GetBoolField(TEXT("AwesomeBoolean"));
float AwesomeFloat = json->GetNumberField(TEXT("AwesomeFloat"));
int32 AwesomeInteger = json->GetNumberField(TEXT("AwesomeInteger"));
FRotator AwesomeRotator = ParseAsRotator(json, FString("AwesomeRotator"));
FAwesomeStruct AwesomeStruct = FAwesomeStruct::BuildAwesomeStruct(
AwesomeVector,AwesomeBoolean,AwesomeFloat,AwesomeInteger,AwesomeRotator
);
AwesomeStructs.Push(AwesomeStruct);
}
}
Array 읽기
TArray<TSharedPtr<FJsonValue>> Array = JsonObject->GetArrayField(TEXT("FieldName"));
또는
const TArray<TSharedPtr<FJsonValue>>* Value;
if(JsonObject->TryGetArrayField(TEXT("FieldName"), Value)) {
// When Parsing Success.
} else {
// When Parsing Failed.
}
ref : https://ballbot.tistory.com/45
ref : https://www.reddit.com/r/unrealengine/comments/shpgbw/json_array_to_unreal_engine_c_or_varest/
ref : https://gist.github.com/hanzochang/07eba255ce0d1695582a92e98e973200
반응형
'게임엔진(GameEngine) > Unreal4' 카테고리의 다른 글
Android 에서 Java -> 호출 (0) | 2022.10.06 |
---|---|
UE4 RPC - Replicate (0) | 2022.09.15 |
순수 가상함수 사용 한는 방법? (0) | 2022.08.26 |
This Repository shows a Code Example of how to use UE4 Online Subsystem Sessions in C++. (0) | 2022.08.04 |
What does Get Player Controller (Player index = 0) return in multiplayer? (0) | 2022.07.27 |