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"

FJsonSerializer::Deserialize(reader, JsonObj) - fails every time

Hello all - been over a MULTITUDE of answerhub posts and wiki docs to try to get JSON deserialization happening on my end.... with no luck. Below is my code, it always fails the Deserialize() function. All headers are included properly - this is purely the ability to load a JSON object and read from it - which i cant get working :( My output is coming from a PHP file i built - shown below as well.

 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))
 {
     UE_LOG(LogTemp, Warning, TEXT("JSON IS VALID"));
     // All is okay, json is valid
     //TSharedPtr<FJsonObject> jsonObj = JsonObj->GetObjectField("array");
     UE_LOG(LogTemp, Warning, TEXT("ARRAY SIZE FOR ARRAYFIELD: %s"), JsonObj->GetArrayField("AchievementResults").Num());
     //TArray<TSharedPtr<FJsonValue>> OutArray = JsonObj->GetArrayField("AchievementResults");
     //for (int32 i = 0; i < OutArray.Num(); i++)
     //{
     //    UE_LOG(LogTemp, Warning, TEXT("%s"), *OutArray[i]->AsString());
     //}
     
 }
 else UE_LOG(LogTemp, Error, TEXT("Unable to read JSON Data"));

PHP JSON OUTPUT FROM WEB:

{"Status":"1","AchievementResults":{"ID":"0","TaskType":"kill","ClassID":"ClassStuffHere","Amount_Current":"0","Amount_Needed":"25"}}

Product Version: UE 4.16
Tags:
more ▼

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

avatar image

walldiv
117 8 11 22

avatar image kodusu Oct 04 '17 at 05:39 AM

Hey your code seems to be correct but check the Response code this might help you for better debugging your issue and 1st try to get the simple fields like Status..!! Here is the sample code i used for debugging in my project

 void AJsonDownloader::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
 {
     if (bWasSuccessful)
     {    
         TSharedPtr<FJsonObject> JsonParsed;
         TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(Response->GetContentAsString());
         if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
         {
             FString Status= JsonParsed->GetStringField("Status");    
              }
     if (!Response.IsValid()) {
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Invalid Http Response..!!")));
         UE_LOG(LogTemp, Warning, TEXT("Invalid Http Response..!!"));
     }
     else if (Response->GetResponseCode() == 200) {
         FString msg = Response->GetContentAsString();
         
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT(" Valid Http Response.. %s"), *msg));
         UE_LOG(LogTemp, Warning, TEXT("%s"), *msg);
     }
     else {
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Error code %d"), Response->GetResponseCode()));
         UE_LOG(LogTemp, Warning, TEXT("%d"), Response->GetResponseCode());
     }
 
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

This acutally turned out to be a RETURN OBJECT problem - i was returning more than 1 row in my JSON response.... which i'll have to learn to deal with in a few - but the code worked fine when i only returned 1 row instead of many.

As for the multiple rows response - i'm trying to FString::ParseIntoArray(&OutArray, Delims, false).... but keep the delimiters.... anyone know how to do this?

more ▼

answered Oct 04 '17 at 07:16 AM

avatar image

walldiv
117 8 11 22

(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