我正在对在线API进行GET请求,但无法将嵌套的Json值设置为C ++(虚幻引擎4)中的数组。 我得到了除嵌套数组 LE (键)
以外的所有值这是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
}
]
}
这是重要的代码
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();
}
}
我可以打印所有(LE)索引测试if (index == 0)
,但不能将其放入数组。
我真的不知道该怎么办,我整日都被困在这里。
答案 0 :(得分:1)
您尝试过以下方法吗:
TArray<TSharedPtr<FJsonValue>> arr = JsonObject->GetArrayField("LE");
for (int32 index = 0; index < arr.Num(); index++)
{
TSharedPtr<FJsonObject> obj = arr[index]->AsObject();
/* get the object's fields:
obj->GetStringField("Nome");
*/
}