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"

FHttpResponsePtr - MULTIPLE Rows returned

How do I handle multiple rows returned from my JSON server response? This could be N# of rows from an SQL Select statement - consider Inventory, abilities, loadout etc....

So far - i can only seem to get 1 return object to function with the FJsonSerializer::Deserialize(Reader, JsonObj).

I am getting an FHttpResponsePtr return - and doing: Response->GetContentAsString(); which is the only thing i can see as a possible way to handle JSON response from database from the web. Perhaps theres a better alternative? CODE:

 TSharedPtr<FJsonObject> JsonObj = MakeShareable(new FJsonObject());
 
     FString MsgBody = Response->GetContentAsString();
     UE_LOG(LogTemp, Warning, TEXT("%s"), *MsgBody);
 
     TSharedRef<TJsonReader<TCHAR>> Reader = TJsonReaderFactory<TCHAR>::Create(*MsgBody);
     if (FJsonSerializer::Deserialize(Reader, JsonObj))
     {
         TSharedPtr<FJsonObject> AchObj = JsonObj->GetObjectField("Achievements");
         UE_LOG(LogTemp, Warning, TEXT("JSON IS VALID"));
         // All is okay, json is valid
         FString ID = AchObj->GetStringField(TEXT("ID"));
         FString TaskType = AchObj->GetStringField(TEXT("TaskType"));
         FString ClassID = AchObj->GetStringField(TEXT("ClassID"));
         FString Amount_Current = AchObj->GetStringField(TEXT("Amount_Current"));
         FString Amount_Needed = AchObj->GetStringField(TEXT("Amount_Needed"));
 
         UE_LOG(LogTemp, Warning, TEXT("%s %s %s %s %s"), *ID, *TaskType, *ClassID, *Amount_Current, *Amount_Needed);
     }


JSON RESPONSE: {"Achievements":{"ID":"0","TaskType":"kill","ClassID":"ClassStuffHere","Amount_Current":"0","Amount_Needed":"25"}}{"Achievements":{"ID":"1","TaskType":"kill","ClassID":"bears","Amount_Current":"0","Amount_Needed":"50"}}

Product Version: UE 4.16
Tags:
more ▼

asked Oct 04 '17 at 08:58 AM in C++ Programming

avatar image

walldiv
114 7 11 21

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

1 answer: sort voted first

Turns out this was a PHP formatting issue - i was able to format a proper JSON Array from php with the following - and then successfully pull off a JSON reader with the code below the php.

PHP:

 function resultToArray($result) {
     $rows = array();
     while($row = $result->fetch_assoc()) {
         $rows[] = array(
                 'ID' => $row['ID'],
                 'TaskType' => $row['TaskType'],
                 'ClassID' => $row['ClassID'],
                 'Amount_Current' => $row['Amount_Current'],
                 'Amount_Needed' => $row['Amount_Needed']
             );
     };
     return $rows;
 }
 
 //DB ACTION
 $stmt = "SELECT * FROM achievementscompleted a, accounts b WHERE a.AccountFK = b.id AND b.username = '$pName'";
 $result = $con->query($stmt);
 $rows = resultToArray($result);
 if($rows)
     echo json_encode(array("Achievements" => $rows));


.CPP

 TSharedPtr<FJsonObject> JsonObj = MakeShareable(new FJsonObject());

 FString MsgBody = Response->GetContentAsString();
 TSharedRef<TJsonReader<TCHAR>> Reader = TJsonReaderFactory<TCHAR>::Create(*MsgBody);
 if (FJsonSerializer::Deserialize(Reader, JsonObj))
 {
     TArray<TSharedPtr<FJsonValue>> ArrayObj = JsonObj->GetArrayField("Achievements");
     UE_LOG(LogTemp, Warning, TEXT("ARRAY IS VALID WITH AMOUNT OF : %d"), ArrayObj.Num());

 }

more ▼

answered Oct 05 '17 at 06:31 PM

avatar image

walldiv
114 7 11 21

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
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