Hi,
I can’t deserialize a JSon request.
the .h :
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/GameMode.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "FICEGameMode.generated.h"
/**
*
*/
UCLASS()
class FICE_API AFICEGameMode : public AGameMode
{
GENERATED_BODY()
TSharedPtr<FJsonObject> JsonObject;
FHttpModule* Http;
/* The actuall 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
AFICEGameMode(const class FObjectInitializer& ObjectInitializer);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
The .CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "FICE.h"
#include "FICEGameMode.h"
// Sets default values
AFICEGameMode::AFICEGameMode(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
//When the object is constructed, Get the HTTP module
Http = &FHttpModule::Get();
}
// Called when the game starts or when spawned
void AFICEGameMode::BeginPlay()
{
MyHttpCall();
Super::BeginPlay();
}
/*Http call*/
void AFICEGameMode::MyHttpCall()
{
TSharedRef<IHttpRequest> Request = Http->CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &AFICEGameMode::OnResponseReceived);
//This is the url on which to process the request
Request->SetURL("http://www.localfice.fr/fice3d/httprequest.php");
Request->SetVerb("GET");
Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->ProcessRequest();
}
/*Assigned function on successfull http call*/
void AFICEGameMode::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
//Create a pointer to hold the json serialized data
int responseCode = Response->GetResponseCode();
FString s = "OnResponseReceived : " + FString::FromInt(responseCode);
GEngine->AddOnScreenDebugMessage(-1, 12.0f, FColor::Green, Response->GetContentAsString());
//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))
{
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Green, "Deserialized");
//Get the value of the json object by field name
//int32 recievedInt = JsonObject->GetIntegerField("customInt");
int i = 0;
for (TPair<FString, TSharedPtr<FJsonValue>> & Elem : JsonObject->Values)
{
GEngine->AddOnScreenDebugMessage(i, 2.0f, FColor::Blue, Elem.Value->AsString());
i++;
}
//Output it to the engine
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, "NOT Deserialize");
}
}
The .PHP
<?php
//Create a variable to be used in
$theVar = array();
$host = “127.0.0.1”;
$user = “root”;
$psw = “”;
$db = “fice”;
$connection = mysql_connect($host, $user, $psw) or die("Erreur de connection à la base de données");
mysql_select_db($db, $connection);
$query = "SELECT * FROM categories WHERE categorie=0 ORDER BY nom";
$result = mysql_query($query) or die(mysql_error());
while($obj = mysql_fetch_object($result))
{
$theVar[$obj->id] = $obj;
}
//Set the headers
header('Content-Type: application/json');
//Encode the variable, and save the encoded string
$encoded = json_encode($theVar);
//Output it
echo $encoded;
?>
The returned string (as shown by direct access to the PHP in chrome, without unreal)
{“3”:{“id”:“3”,“nom”:“Festival Astro-Jeune”,“soustitre”:"",“description”:null,“texte”:“7”,“image”:"",“baniere”:“astrojeune-2014.jpg”,“categorie”:“0”,“style”:""},“2”:{“id”:“2”,“nom”:“Festival d’Astronomie de Fleurance”,“soustitre”:"",“description”:"",“texte”:“0”,“image”:"",“baniere”:“festival-2014.jpg”,“categorie”:“0”,“style”:""},“4”:{“id”:“4”,“nom”:null,“soustitre”:"",“description”:"",“texte”:“0”,“image”:"",“baniere”:“evenements-2014.jpg”,“categorie”:“0”,“style”:""},“1”:{“id”:“1”,“nom”:“Marathon des Sciences”,“soustitre”:null,“description”:null,“texte”:“1”,“image”:"",“baniere”:“marathon-2016.jpg”,“categorie”:“0”,“style”:""}}