HoyangUS
(HoyangUS)
1
I wanna generate json file with two-dimensional array as a property like below.
{
“two_dim_array” : [ [0.0, 0.1], [1.0, 1.1] ]
}
How can i generate this json on UE ?
The example of one-dimensional array.
bool UGenerateTwoDimArrayiJson::GenerateTwoDimArrayJsonToFile(FString FilePath) {
TSharedPtr<FJsonObject> JsonRootObject = MakeShareable(new FJsonObject);
TArray<TSharedPtr<FJsonValue>> one_dim_array;
one_dim_array.Add(MakeShareable(new FJsonValueNumber(0.0)));
one_dim_array.Add(MakeShareable(new FJsonValueNumber(0.1)));
one_dim_array.Add(MakeShareable(new FJsonValueNumber(1.0)));
one_dim_array.Add(MakeShareable(new FJsonValueNumber(1.1)));
JsonRootObject->SetArrayField("one_dim_array", one_dim_array);
FString OutPutString;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&OutPutString);
FJsonSerializer::Serialize(JsonRootObject.ToSharedRef(), Writer);
return FFileHelper::SaveStringToFile(OutPutString, *FilePath);
koyama_yu1
(koyama_yu1)
2
I solved like this
TArray<TSharedPtr<FJsonValue>> two_dimensional_Array;
TArray<TSharedPtr<FJsonValue>> first_dim_array;
first_dim_array.Add(MakeShareable(new FJsonValueNumber(0.0)));
first_dim_array.Add(MakeShareable(new FJsonValueNumber(0.1)));
first_dim_array.Add(MakeShareable(new FJsonValueNumber(1.0)));
first_dim_array.Add(MakeShareable(new FJsonValueNumber(1.1)));
TSharedRef<FJsonValueArray> first_dimData = MakeShareable(new FJsonValueArray(first_dim_array));
two_dimensional_Array.Add(firsr_dimData);
TArray<TSharedPtr<FJsonValue>> second_dim_array;
second_dim_array.Add(MakeShareable(new FJsonValueNumber(0.0)));
second_dim_array.Add(MakeShareable(new FJsonValueNumber(0.2)));
second_dim_array.Add(MakeShareable(new FJsonValueNumber(2.0)));
second_dim_array.Add(MakeShareable(new FJsonValueNumber(2.2)));
TSharedRef<FJsonValueArray>second_dimData = MakeShareable(new FJsonValueArray(second_dim_array));
two_dimensional_Array.Add(second_dimData);