I will wait your answer. Thanks)
This is the smallest example you can put together for the built in JSON system:
#include "Json.h"
void FJsonExample::ParseExample()
{
FString JsonRaw = "{ \"exampleString\": \"Hello World\" }";
TSharedPtr<FJsonObject> JsonParsed;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonRaw);
if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
{
FString ExampleString = JsonParsed->GetStringField("exampleString");
}
}
ExampleString will be āHello Worldā.
There are alternative methods to GetStringField for retrieving other types, including Arrays and Objects.
Hope that helps.
Joe
trying this results in many linker errors LNK2019 and LNK2001 when compiling. I just copy&pasted your code. Am I missing something?
As of 4.6.X you need to include the JSON modules for it to work properly.
PublicDependencyModuleNames.AddRange(new string[] { āJsonā, āJsonUtilitiesā });
This goes in your GameProject.Build.cs file.
Thanks Joeh, missed that one. Works great.
Hi Joeh,
I would appreciate it very much if you could have a look at my problem as well - Iāve created a new project, no new classes, added Json dependency modules but for the life of me I cannot get it to compile - any ideas?:
TestGame.Build.cs:
// Fill out your copyright notice in the Description page of Project Settings.
using UnrealBuildTool;
public class TestGame : ModuleRules
{
public TestGame(TargetInfo Target)
{
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore"});
PublicDependencyModuleNames.AddRange(new string[] { "Json", "JsonUtilities" });
PrivateDependencyModuleNames.AddRange(new string[] { });
}
}
TestGameGameMode.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "TestGame.h"
#include "TestGameGameMode.h"
#include "Json.h"
ATestGameGameMode::ATestGameGameMode(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
TSharedPtr<FJsonObject> j;
// The following line causes linker errors
j = MakeShareable(new FJsonObject);
}
TestGameGameMode.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Json.h"
#include "GameFramework/GameMode.h"
#include "TestGameGameMode.generated.h"
/**
*
*/
UCLASS()
class TESTGAME_API ATestGameGameMode : public AGameMode
{
GENERATED_BODY()
public:
ATestGameGameMode(const FObjectInitializer& ObjectInitializer);
};
Well Iāll be a monkeyās posterior - I didnāt know that I had to rebuild my game to update the included modules. Everything is working now.
in 4.13 compilation of example is failed with
Unrecognized type āFJsonObjectā - type must be a UCLASS, USTRUCT or UENUM