Read json file to struct
Nested object supported
example.json
json
{
"scale":
{
"x": "1.0",
"y": "1.0",
"z": "1.0"
}
}
cpp
/** required module
* "Json",
* "JsonUtilities"
*/
FString JsonContent;
FFileHelper::LoadFileToString(jsonContent, *JsonPath);
FOurCustomEntity entity;
if (!FJsonObjectConverter::JsonObjectStringToUStruct<FOurCustomEntity>(jsonContent, &entity))
{
UE_LOG(LogTemp, Error, TEXT("ERROR When execute JsonObjectStringToUStruct on response."));
return false;
}
UE_LOG(LogTemp, Error, TEXT("TestScale: %s"), entity.scale.x);
Struct Example
cpp
#pragma once
#include "StructModels.generated.h"
USTRUCT()
struct FScale
{
GENERATED_BODY()
public:
UPROPERTY()
FString x;
UPROPERTY()
FString y;
UPROPERTY()
FString z;
};
USTRUCT()
struct FOurCustomEntity
{
GENERATED_BODY()
public:
UPROPERTY()
FScale scale;
};