-
-
Save pkucherov/71dbb7c285e4c72dbc8072d2f617a522 to your computer and use it in GitHub Desktop.
How to parse a json response (e.g. a json array) in ue4 c++ context.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Runtime/Online/HTTP/Public/Http.h" | |
#include "Serialization/JsonSerializer.h" | |
FHttpResponsePtr Response; | |
{ | |
// Single json value (any of supported json types e.g. | |
// object with properties, array, bool) at top level of json | |
TSharedPtr<FJsonValue> JsonValue; | |
//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, JsonValue)) { | |
//Get the value of the json object by field name | |
bool status = JsonValue->AsObject()->GetBoolField("status"); | |
} | |
} | |
{ | |
// Array of json objects at top level of json | |
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 | |
FString name = JsonObject->GetStringField("name"); | |
} | |
} | |
{ | |
// Array of json objects at top level of json | |
TArray<TSharedPtr<FJsonValue>> JsonArray; | |
//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, JsonArray)) { | |
//Get the value of the json object by field name | |
FString name = JsonArray[0]->AsObject()->GetStringField("name"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment