Greetings all,
Currently I have got a json file from the Met Office printed in its entirety to the screen as a string but I wish to be able to print selected values from the json to the screen. As the json is nested I have constructed USTRUCTS to store the converted values to. For example:
USTRUCT()
struct FJsonData
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
FSiteRep SiteRep;
UPROPERTY()
FWx Wx;
UPROPERTY()
TArray<FParam> Param;
UPROPERTY()
FDV DV;
UPROPERTY()
FLocation Location;
UPROPERTY()
TArray<FPeriod> Period;
UPROPERTY()
FRep Rep;
};
This is the main USTRUCT which contains the other USTRUCTs. The other USTRUCTs contain FString variables to store the values from the JsonString.
My problem lies in that my UStructs are empty and I am uncertain how to populate them with the relevant values from the JSonObject. Here is my method so far.
void AHttpActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
//Create a pointer to hold the json serialized data
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
//Store Json as string
FString JsonString = Response->GetContentAsString();
//Create a reader pointer to read the json data
TSharedRef<TJsonReader<TCHAR>> Reader = TJsonReaderFactory<TCHAR>::Create(JsonString);
//Deserialize the json data given Reader and the actual object to deserialize
if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
//Convert JsonObjectString to UStruct
FJsonData JsonData;
FJsonObjectConverter::JsonObjectStringToUStruct<FJsonData>(
JsonString,
&JsonData,
0, 0);
//Get the value of the json object by field name
JsonData.Location.country = JsonObject->GetStringField(TEXT("country"));
//Outputs Json as a string
GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Yellow, FString(JsonString));
//Outputs nothing
GEngine->AddOnScreenDebugMessage(5, 5.0f, FColor::Green, FString(JsonData.Location.country));
}
else
{
GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Red, FString("error"));
}
}
Hit a blocker with this and was hoping for some assistance; any help would be appreciated.