How I can parse JSON data with a standard rountines in UE4? Take me please a sample

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

2 Likes

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.

@Joeh Can you refer some website or link where more of this can be learned? Like parsing Arrays

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