Download

Working with JSON

Hello there,

I’ve been trying to work with JSON and I was wondering why this is not working? The value seems to not get set at all.
void AMyCharacter::TryJson()
{
FString TheStuffIGotFromTheServer;
TheStuffIGotFromTheServer = “{foo: “bar”, userid: 31}”;
TSharedPtr<FJsonObject> ParsedJson;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(TheStuffIGotFromTheServer);
if (FJsonSerializer::Deserialize(JsonReader, ParsedJson))
{
FString foo = ParsedJson->GetStringField(“foo”);
double UserId = ParsedJson->GetNumberField(“userid”);
SomeValue = foo;
}
}

Best regards


{foo: "bar", userid: 31}

  • is not a valid json.
    Valid one is:

{"foo" : "bar", "userid" : 31}

Thanks for pointing that out.

I have another question; I’ve gone a different route in my testing and started making HTTP POST calls (or attempting to).
I was wondering if there was any kind I could test by writing raw text into SetContentAsString() - if so, how would I format it?
This is what I’ve got so far.

TSharedRef<IHttpRequest> Request = Http->CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &AMyCharacter::OnResponseRecieved);
Request->SetURL(“http://localhost/strafegame/index.php”);
Request->SetVerb(“POST”);
//Request->SetContentAsString(“hello=world”);
Request->SetHeader(TEXT(“User-Agent”), “X-UnrealEngine-Agent”);
Request->SetHeader(“Content-Type”, TEXT(“application/json”));
Request->ProcessRequest();

The HTTP call is working properly but it’s not setting the POST variables (obviously since I don’t know how to set them).
Any help would be appreciated!