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"

Accessing array of object after deserialization

Hi,

I'm new to UE4. I'm trying HTTP GET request in using c++ code. I'm able to fetch the response but unable to access array of object after deserializing it.

Here is my response obtained from get request

  {
 "person": [{
     "first_name": "Paul",
     "referal_code": "PP006",
     "last_name": "James"
 }, {
     "first_name": "Admans",
     "referal_code": "PP014",
     "last_name": "Peter"
 }]
 }

Here is my code snippet

<-------------HTTPActor.h file------------------------->

 #pragma once
 #include "GameFramework/Actor.h"
 #include "Runtime/Online/HTTP/Public/Http.h"
 #include "HTTPActor.generated.h"

 USTRUCT()
 struct FPersonInfo
 {
 GENERATED_USTRUCT_BODY()

     UPROPERTY()
     FString first_name;

 UPROPERTY()
     FString referal_code;

 UPROPERTY()
     FString last_name;
 };


 UCLASS()
 class 3D_API AHTTPActor : public AActor
 {
 GENERATED_BODY()

 public:
 FHttpModule* Http;
 /* The actual HTTP call */
 UFUNCTION()
     void MyHttpCall();

 /*Assign this function to call when the GET request processes sucessfully*/
 void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);

 // Sets default values for this actor's properties
 AHTTPActor();

 // Called when the game starts or when spawned
 virtual void BeginPlay() override;
 };



<-------------HTTPActor.cpp file------------------------->

 #include "3D.h"
 #include "HTTPActor.h"
 #include <array>
 #include <string>

 // Sets default values
 AHTTPActor::AHTTPActor()
 {
 //When the object is constructed, Get the HTTP module
 Http = &FHttpModule::Get();
 }

 // Called when the game starts or when spawned
 void AHTTPActor::BeginPlay()
 {
  MyHttpCall();
 Super::BeginPlay();
 }

 /*Http call*/
 void AHTTPActor::MyHttpCall()
 {
 TSharedRef<IHttpRequest> Request = Http->CreateRequest();
 Request->OnProcessRequestComplete().BindUObject(this, &AHTTPActor::OnResponseReceived);
 Request->SetURL("My URL for Request");
 Request->SetVerb("GET");
 Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
 Request->SetHeader("Content-Type", TEXT("application/json"));
 Request->ProcessRequest();
 }

 void AHTTPActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool     bWasSuccessful)
 {
 //Declare json as string variable
 FString JsonString = Response->GetContentAsString();
 //Create a pointer to hold the json serialized data
 TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
 //Create a reader pointer to read the json data
 TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString);
 
 //Deserialize the json data given Reader and the actual object to deserialize
 if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
 {
     
     TArray<TSharedPtr<FJsonValue>> mainArray = JsonObject->GetArrayField("person");


                    // HOW TO ACCESS MY OBJECT FROM ARRAY HERE?
 }
 else
 {
     GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Red, FString("error, failed to deserialize"));
 }
 }

I'm unable to access the values of array after completion of request.What am i doing wrong ? Any help will be appricated.

Product Version: UE 4.14
Tags:
more ▼

asked Jan 12 '17 at 04:55 AM in C++ Programming

avatar image

HarshMakadia
13 5 8 9

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

2 answers: sort voted first

Lucky for you I just finished coding this exact thing. This code goes on line 48 of your CPP file:

 TArray<FPersonInfo> PersonInfos;
 
 for (int RowNum = 0; RowNum != mainArray.Num(); RowNum++) {
     FPersonInfo tempPersonInfo;
     TSharedPtr<FJsonObject> tempRow = mainArray[RowNum]->AsObject();
     tempPersonInfo.first_name = tempRow ->GetStringField("first_name");
     tempPersonInfo.referal_code = tempRow ->GetStringField("referal_code");
     tempPersonInfo.last_name = tempRow ->GetStringField("last_name");    
     PersonInfos.Add(tempPersonInfo);
 }

Then in my system I take the PersonInfos array and pass it to a blueprintable event so it can be consumed in blueprints, but you can do whatever you want with it.

more ▼

answered Jan 13 '17 at 04:07 AM

avatar image

Dartanlla
2.6k 62 27 281

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

@Dartanlla Thanks a ton! Worked completely fine.

more ▼

answered Jan 13 '17 at 04:27 AM

avatar image

HarshMakadia
13 5 8 9

(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