C++ 如何在C+中解析JSON文件中的数组数组+;使用虚幻
以下是我试图解析的JSON文件:-C++ 如何在C+中解析JSON文件中的数组数组+;使用虚幻,c++,unreal-engine4,C++,Unreal Engine4,以下是我试图解析的JSON文件:- { "GameSetting":{ "Num_Levels":"3", "Env_Type":"Indoor" }, "Indoor":[ { "Name":"simple room", "objects":[ {"objectName":"chair"}, {"objectName":"tables"}
{
"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"
}
]
}
我使用的是问题答案中规定的方法:
我成功地访问了GameSetting的两个属性:室内的名称和数量、室外的名称、数量、风和天气属性。我唯一遇到问题的属性是室内和室外的对象阵列
以下是我的头文件:
#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;
};
#包括“coremiminal.h”
#包括“GameFramework/Actor.h”
#包括“JsonParser.generated.h”
USTRUCT()
结构FGameSetting
{
生成的目录体()
连根拔起
FString Num_级别;
连根拔起
F管柱环境类型;
};
USTRUCT()
结构FArrayBasic
{
生成的目录体()
连根拔起
FString对象名;
};
USTRUCT()
结构FIndoorBasic
{
生成的目录体()
连根拔起
FString名称;
焦油物体;
连根拔起
f字符串\u对象的编号\u;
};
USTRUCT()
基础结构
{
生成的目录体()
连根拔起
FString名称;
焦油物体;
连根拔起
f字符串\u对象的编号\u;
连根拔起
天气晴朗;
连根拔起
弦风;
};
USTRUCT()
结构物
{
生成的目录体()
连根拔起
FGameSetting游戏设置;
连根拔起
柏油树;
连根拔起
柏油树;
};
UCLASS()
类JSONPARSING_API AJsonParser:public AActor
{
生成的_BODY()
公众:
//设置此参与者属性的默认值
AJsonParser();
受保护的:
//在游戏开始或生成时调用
虚拟void BeginPlay()覆盖;
公众:
//调用每帧
虚拟空刻度(浮动增量时间)覆盖;
};
和.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);
}
//在项目设置的说明页中填写版权声明。
#包括“JsonParser.h”
#包括“JsonUtilities.h”
//设置默认值
AJsonParser::AJsonParser()
{
//将此参与者设置为每帧调用Tick()。如果不需要,可以关闭此选项以提高性能。
PrimaryActorTick.bCanEverTick=true;
}
//在游戏开始或生成时调用
void AJsonParser::BeginPlay()
{
Super::BeginPlay();
const FString JsonFilePath=FPaths::ProjectContentDir()+“/JsonFiles/randomgenerated.json”;
FString JsonString;//Json已转换为FString
FFILHELPER::LoadFileToString(JsonString,*JsonFilePath);
//在输出日志中以字符串格式显示json
GLog->Log(“Json字符串:”);
GLog->Log(JsonString);
//创建一个json对象来存储json字符串中的信息
//json读取器稍后用于反序列化json对象
TSharedPtr JsonObject=MakeShareable(新的FJsonObject());
TSharedRef JsonReader=TJsonReaderFactory::Create(JsonString);
if(FJsonSerializer::反序列化(JsonReader,JsonObject)&&JsonObject.IsValid()
{
主干道;
FJsonObjectConverter::JSONObjectStringToStruct(JsonString和Main,0,0);
FString Data=Main.Outdoor[0]。风;
//FArrayBasic Data1=Main.indior[0].objects.GetData[0];
GLog->Log(“sfdjngejbfwjqwnoesfjkesngkwbegjkwefbnjk”);
GLog->Log(数据);
//对于(int32 index=0;indexLog(“名称:”+Data[index]->AsString());
//}
//从给定json文件中检索的人员“对象”
TSharedPtr GameSetting=JsonObject->GetObjectField(“GameSetting”);
//获得各种属性
GLog->Log(“环境类型:+GameSetting->GetStringField(“环境类型”);
GLog->Log(“NUM_LEVELS:+FString::FromInt(GameSetting->GetIntegerField(“NUM_LEVELS”));
//检索数组属性并打印每个字段
/*TArray objArray=PersonObject->GetArrayField(“家族”);
GLog->Log(“打印姓氏…”);
对于(int32 index=0;indexLog(“名称:”+objArray[index]->AsString());
}*/
}
其他的
{
GLog->Log(“无法反序列化”);
}
}
//调用每帧
void AJsonParser::Tick(float DeltaTime)
{
超级:滴答声(DeltaTime);
}
如果有人能帮我用同样的方法解析JSON文件中数组中的数组,那将非常有帮助。提前谢谢。解决了这个问题
我只需要做些准备
TArray