VortexFX
(VortexFX)
July 4, 2019, 10:11am
1
Hello all,
This is probably a real simple thing but…
I’m trying to convert an array of strings to JSON string so I can send it over to an api.
TArray items;
items.Add(“Test”);
items.Add(“Test2”);
I have looked at this FJsonObjectConverter | Unreal Engine Documentation
but can’t see anything that supports this. I am not experienced at C++ in any shape or form.
basically, I need this to be returned in a string
[“Test”,“Test”]
Many thanks in advance.
zompi2
(zompi2)
July 4, 2019, 11:47am
2
To work with JSON in UE4 You must work with FJsonObject and FJsonValue. For example:
TArray<TSharedPtr<FJsonValue>> items;
items.Add(MakeShareable(new FJsonValueString("Test")));
items.Add(MakeShareable(new FJsonValueString("Test2")));
TSharedRef<FJsonObject> JsonRootObject = MakeShareable(new FJsonObject);
JsonRootObject->SetArrayField("MyArray", items);
FString OutputString;
TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString);
FJsonSerializer::Serialize(JsonRootObject, Writer);
Will produce “MyArray”: [“Test”, “Test2”] json. I don’t know if it is possible to make pure array in ue4 json parser, it always must have a root object.
This will produce a pretty json, if you want to have condensed json use the following writer:
TSharedRef< TJsonWriter<TCHAR, TCondensedJsonPrintPolicy<TCHAR>> > Writer = TJsonWriterFactory<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>::Create(&OutputString);
Other way is to serialize whole structs. For a struct like this:
USTRUCT()
struct FMyStruct
{
GENERATED_BODY()
UPROPERTY()
TArray<FString> Strings;
};
UPROPERTY()
FMyStruct MyStruct;
Filled with:
MyStruct.Strings.Add("Test");
MyStruct.Strings.Add("Test2");
You can run:
TSharedRef<FJsonObject> OutJsonObject = MakeShareable(new FJsonObject);
FJsonObjectConverter::UStructToJsonObject(FMyStruct::StaticStruct(), &MyStruct, OutJsonObject, 0, 0);
FString OutputString;
TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString);
FJsonSerializer::Serialize(OutJsonObject, Writer);
Hope it will help
VortexFX
(VortexFX)
July 4, 2019, 5:21pm
3
Thanks for the help, I get issues trying your second option
‘Member variable declaration’ is not allowed before the Class definition
Any ideas?
#pragma once
#include "Engine.h"
USTRUCT()
struct FQueueTypes {
GENERATED_BODY()
UPROPERTY()
TArray<FString> types;
FQueueTypes() {}
};
UPROPERTY()
FQueueTypes QueueTypes;
zompi2
(zompi2)
July 4, 2019, 6:15pm
4
It was a mental shortcut from my side. The
UPROPERTY()
FQueueTypes QueueTypes;
is supposed to be inside a class definition. Like for example…
UCLASS()
class ASomeActor : public AActor
{
GENERATED_BODY()
UPROPERTY()
FQueueTypes QueueTypes;
void BeginPlay() override
{
Super::BeginPlay();
// Do a serialization / deserialization here
}
};
VortexFX
(VortexFX)
July 10, 2019, 9:08am
5
Thanks very much for your help
For reference, if anybody is interested, of course you can serialize without a root object. I you skip the root object part of the code and directly use the TArray
of FJsonValueString
objects as an argument for the serialization, it will produce: [“Test”, “Test2”]
The code becomes:
TArray<TSharedPtr<FJsonValue>> items;
items.Add(MakeShareable(new FJsonValueString("Test")));
items.Add(MakeShareable(new FJsonValueString("Test2")));
FString OutputString;
TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString);
FJsonSerializer::Serialize(items, Writer);
1 Like