【发布时间】:2019-03-02 04:26:52
【问题描述】:
以下是我要解析的 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 的 Name 和 number_of_objects 属性以及 Outdoor 的 Name、number_of_objects、Wind 和 Weather 属性。我唯一遇到问题的属性是 Indoor 和 Outdoor 中的对象数组。
以下是我的头文件:
#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 文件中的数组中的数组,那将非常有帮助。提前致谢。
【问题讨论】:
-
当你做 Main.Indoor[0].objects.GetData[0];您是否尝试访问 Main.Indoor[0].objects[0].objectName; ?
-
@AndreaAbbate 是的。发动机崩溃。我浏览了日志,发现了这一点:[2018.09.26-11.51.41:993][931]LogWindows: Error: Assertion failed: (Index >= 0) & (Index
标签: c++ unreal-engine4