Hi all… I’ve been searching for an a post that already answers this but not having any luck.
I have a need to work with JSON data. I’ve been able to use the JSON utilities for most cases however, there is one case that I’m not finding what I need exactly…
I have these two structures, the first struct being consumed in the second struct.
USTRUCT(BlueprintType, Category = "REST API")
struct FRestAPI_TestName
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, Category = "REST API")
FString testName = "";
};
USTRUCT(BlueprintType, Category = "REST API")
struct FRestAPI_Test_Category
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, Category = "REST API")
FString categoryName = "";
UPROPERTY(BlueprintReadWrite, Category = "REST API")
TArray<FRestAPI_TestName> testNames;
};
I can use the UStructToJsonObjectString() no problem with an instance of a FRestAPI_Test_Category instance.
However, I need to be able to convert a TArray<FRestAPI_Test_Category> into a JSON string… I have a function body that looks similar to this.
void MyClass::SomeFunc( TArray<FRestAPI_Test_Category> testCategories )
{
FString postBody;
if( FJsonObjectConverter::UStructToJsonObjectString( testCategories, postBody) )
{
...Do something...
}
}
I get a compile time error, I’m assuming it’s because the UStructToJsonObjectString can’t handle a TArray as an input parameter. I’m not finding a function that can take a TArray of structs and covert that.
So, I ended up having to create a utility function that will iterate over each element in the array, convert each FRestAPI_Test_Category struct and append it to a master string, and then also manipulate some of the brackets and what not to make it a properly formatted JSON string…
FString MyClass::TArrayToJSONString(TArray<FRestAPI_Test_Category> testCategories )
{
FString json = "[";
for (int x = 0; x < tests.Num(); x++)
{
FString postBody;
FJsonObjectConverter::UStructToJsonObjectString( tests[x], postBody );
json += postBody;
// Dont put the comma at the end of the last struct.
if ((x + 1) != tests.Num())
{
json += ",\n";
}
}
json += "]";
return json;
}
Is there a better approach than having to manually create my own utility function? Seems like there would have to be some kind of a converstion function that can already handle a TArray of FStruct types.
Thanks for any help.