I can't get Json Nested Values to TArray

I’m doing a GET Request to an online API but I can’t get nested Json values to set to an array in C++ (Unreal Engine 4). I got all values except nested array LE (key)

Here is JSON

 {
    "Id": 8,
    "Nome": "Name",
    "DataInicial": "2018-10-01T00:00:00",
    "DataFinal": "2018-12-31T00:00:00",
    "VL": 270174.982,
    "CN": 461,
    "Met": 354940,
    "PM": 76.118493829943088972784132529,
    "LE": [
        {
            "Id": 25,
            "Nome": "Suco",
            "Cor": "#510077",
            "ValorNegociacaoGanha": 57772.452,
            "CountNegociacaoGanha": 107,
            "Meta": 66700,
            "PercentualMeta": 86.61537031484257871064467766
        },
        {
            "Id": 23,
            "Nome": "Espumante",
            "Cor": "#edd865",
            "ValorNegociacaoGanha": 52494.03,
            "CountNegociacaoGanha": 93,
            "Meta": 66700,
            "PercentualMeta": 78.701694152923538230884557721
        }
    ]
}

Here is important code

void AHttpActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {

//Create a pointer to hold the json serialized data
TSharedPtr<FJsonObject> JsonObject;

//Create a reader pointer to read the json data
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());

//Deserialize the json data given Reader and the actual object to deserialize
if (FJsonSerializer::Deserialize(Reader, JsonObject)) {

    //Get the value of the json object by field name
    int32 id = JsonObject->GetIntegerField("Id");
    FString nome = JsonObject->GetStringField("Nome");
    FString DataInicial = JsonObject->GetStringField("DataInicial");
    FString DataFinal = JsonObject->GetStringField("DataFinal");
    double VN = JsonObject->GetNumberField("VN");
    int32 CN = JsonObject->GetIntegerField("CN");
    int32 Met = JsonObject->GetIntegerField("Met");
    double PM = JsonObject->GetNumberField("PM");

    //Add data to array
    Arr1.Emplace(FString::FromInt(id));
    Arr1.Emplace(nome);
    Arr1.Emplace(DataInicial);
    Arr1.Emplace(DataFinal);
    Arr1.Emplace(FString::SanitizeFloat(VN));
    Arr1.Emplace(FString::FromInt(CN));
    Arr1.Emplace(FString::FromInt(Met));
    Arr1.Emplace(FString::SanitizeFloat(PM));

    UE_LOG(LogTemp, Warning, TEXT("===== GET LE ====="));
    //FString teste = JsonObject->GetField("LE");
    //UE_LOG(LogTemp, Warning, TEXT(teste));
    //ArrSuco = JsonObject->GetArrayField("LE");
    //TArray<TSharedPtr<FJsonValue>> objArray = JsonObject->GetArrayField("LE");
    //ArrTeste = JsonObject->GetArrayField("LE");
    //GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, "printing LE...");
    UE_LOG(LogTemp, Warning, TEXT("===== printing LE... ====="));
    auto arr = JsonObject->GetArrayField("LE");
    for (int32 index = 0; index < arr.Num(); index++)
    {
        //GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, "name:" + FString(objArray[index]->AsString()));
        UE_LOG(LogTemp, Warning, TEXT("TESTE"));

        //ArrSuco
        if (index == 0) {
            //ArrSuco.Emplace(FString::FromInt();
            UE_LOG(LogTemp, Warning, TEXT("Index = 0"));
            //FString teste = arr[0].Value;
            //auto arr2 = arr[0].Get()->TryGetArray;

            //UE_LOG(LogTemp, Warning, TEXT(arr[0].Get()->AsString()));
            //GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, FString(sucoId));
            //UE_LOG(LogTemp, Warning, TEXT(FString(arr[index]["Id"])));
            //ArrSuco.Append(FString(arr[index]["Id"]), arr[index].Num());
            //ArrSuco.Emplace(FString::FromInt(id));
            //ArrSuco.Emplace(nome);
        }
        //ArrEspumante
        else if (index == 1) {
            UE_LOG(LogTemp, Warning, TEXT("Index = 1"));
        }
        //ArrCerveja
        else if (index == 2) {
            UE_LOG(LogTemp, Warning, TEXT("Index = 2"));
        }
        //ArrVinho
        else if (index == 3) {
            UE_LOG(LogTemp, Warning, TEXT("Index = 3"));
        }

        //UE_LOG(LogTemp, Warning, TEXT(objArray[index]->AsString()));
        //UE_LOG(LogTemp, Warning, TEXT(FString(ArrSuco[index])));
    }

        //Count Array
        //int32 CountArr = ArrSuco.Num();
}

I can print all (LE) indexes tests if (index == 0), but I can’t put it to an array. I really don’t know how to get it, I’m stuck here all the day.

I have same issue with this error message “Error: Json Value of type ‘Array’ used as a ‘String’.” … did you solve your issue ?

1 Like