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