x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.18
Tags:
more ▼

asked Oct 26 '18 at 09:58 PM in C++ Programming

avatar image

alangodoi
1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question