gpt4 book ai didi

c++ - 如何使用 Unreal 在 C++ 中解析 JSON 文件中的 Array of Array

转载 作者:行者123 更新时间:2023-11-30 04:56:24 26 4
gpt4 key购买 nike

以下是我要解析的 JSON 文件:-

{  
"GameSetting":{
"Num_Levels":"3",
"Env_Type":"Indoor"
},
"Indoor":[
{
"Name":"simple room",
"objects":[
{"objectName":"chair"},
{"objectName":"tables"},
{"objectName":"boxes"},
{"objectName":"barrels"}
],
"number_of_objects":"10"
},
{
"Name":"multistory",
"objects":[
{ "objectName":"chair"},
{"objectName":"tables"},
{"objectName":"railings"},
{"objectName":"staircase"}
],
"number_of_objects":"25"
},
{
"Name":"passageway",
"objects":[
{"objectName":"forklift"},
{"objectName":"tables"},
{"objectName":"rooms"},
{"objectName":"doors"}
],
"number_of_objects":"32"
}
],
"Outdoor":[
{
"Name":"Downtown",
"objects":[
{"objectName":"Tall buildings"},
{"objectName":"medium buildings"},
{"objectName":"cars"},
{"objectName":"people"}
],
"number_of_objects":"10",
"Weather":"sunny",
"Wind":"Yes"
},
{
"Name":"Neighborhood",
"objects":[
{"objectName":"houses"},
{"objectName":"complexes"},
{"objectName":"community area"},
{"objectName":"park"},
{"objectName":"cars"},
{"objectName":"people"}
],
"number_of_objects":"25",
"Weather":"rainy",
"Wind":"Yes"
},
{
"Name":"Forest",
"objects":[
{"objectName":"trees"},
{"objectName":"lake"},
{"objectName":"mountains"}
],
"number_of_objects":"32",
"Weather":"rainy",
"Wind":"No"
}
]
}

我正在使用这个问题的答案中指定的方法:C++ Nested JSON in Unreal Engine 4

我能够成功访问 GameSetting 的两个属性,Indoor 的名称和 number_of_objects 属性以及 Outdoor 的名称、number_of_objects、风和天气属性。我遇到的唯一问题是室内和室外的对象数组。

下面是我的头文件:

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "JsonParser.generated.h"

USTRUCT()
struct FGameSetting
{
GENERATED_USTRUCT_BODY()

UPROPERTY()
FString Num_Levels;

UPROPERTY()
FString Env_Type;
};
USTRUCT()
struct FArrayBasic
{
GENERATED_USTRUCT_BODY()

UPROPERTY()
FString objectName;
};
USTRUCT()
struct FIndoorBasic
{
GENERATED_USTRUCT_BODY()

UPROPERTY()
FString Name;

TArray <FArrayBasic> objects;

UPROPERTY()
FString number_of_objects;
};
USTRUCT()
struct FOutDoorBasic
{
GENERATED_USTRUCT_BODY()

UPROPERTY()
FString Name;

TArray <FArrayBasic> objects;

UPROPERTY()
FString number_of_objects;

UPROPERTY()
FString Weather;

UPROPERTY()
FString Wind;
};
USTRUCT()
struct FMain
{
GENERATED_USTRUCT_BODY()

UPROPERTY()
FGameSetting GameSetting;

UPROPERTY()
TArray<FIndoorBasic>Indoor;

UPROPERTY()
TArray<FOutDoorBasic>Outdoor;

};

UCLASS()
class JSONPARSING_API AJsonParser : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
AJsonParser();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;


public:
// Called every frame
virtual void Tick(float DeltaTime) override;



};

和 .cpp 文件:

// Fill out your copyright notice in the Description page of Project Settings.

#include "JsonParser.h"
#include "JsonUtilities.h"

// Sets default values
AJsonParser::AJsonParser()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AJsonParser::BeginPlay()
{
Super::BeginPlay();

const FString JsonFilePath = FPaths::ProjectContentDir() + "/JsonFiles/randomgenerated.json";
FString JsonString; //Json converted to FString

FFileHelper::LoadFileToString(JsonString,*JsonFilePath);


//Displaying the json in a string format inside the output log
GLog->Log("Json String:");
GLog->Log(JsonString);

//Create a json object to store the information from the json string
//The json reader is used to deserialize the json object later on
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(JsonString);

if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
{

FMain Main;
FJsonObjectConverter::JsonObjectStringToUStruct<FMain>(JsonString, &Main, 0, 0);

FString Data = Main.Outdoor[0].Wind;
//FArrayBasic Data1 = Main.Indoor[0].objects.GetData[0];
GLog->Log("sfdjngejbfwjqwnoesfjkesngkwbegjkwefbnjk");
GLog->Log(Data);


//for (int32 index = 0; index<Data.Num(); index++)
//{
// GLog->Log("name:" + Data[index]->AsString());
//}


//The person "object" that is retrieved from the given json file
TSharedPtr<FJsonObject> GameSetting = JsonObject->GetObjectField("GameSetting");

//Getting various properties
GLog->Log("ENV_TYPE:" + GameSetting->GetStringField("ENV_TYPE"));
GLog->Log("NUM_LEVELS:" + FString::FromInt(GameSetting->GetIntegerField("NUM_LEVELS")));

//Retrieving an array property and printing each field
/*TArray<TSharedPtr<FJsonValue>> objArray=PersonObject->GetArrayField("family");
GLog->Log("printing family names...");
for(int32 index=0;index<objArray.Num();index++)
{

GLog->Log("name:"+objArray[index]->AsString());
}*/
}
else
{
GLog->Log("couldn't deserialize");
}

}

// Called every frame
void AJsonParser::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

如果有人可以使用相同的方法帮助我解析 JSON 文件中数组中的数组,那将非常有帮助。提前致谢。

最佳答案

想通了。

我只需要让TArray < FArrayBasic > objects 一个 UPROPERTY() 让它反射(reflect)在这个系统中并获取复制到它的值,这样我们就可以像我试图访问它的那样访问它.

关于c++ - 如何使用 Unreal 在 C++ 中解析 JSON 文件中的 Array of Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52516942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com