如何访问嵌套的JSON中的动态嵌套JSON(我不知道预定的值和键)
问候我的堆放者。 我致力于重新创建Minecraft以团结的形式(是的,我确实知道Mineways,可以通过.OBJ - > .fbx做到这一点)为什么我想这样做仅仅是因为我想要所有的块要使用户与用户进行互动简单(仅输入保存构建和TA-DAAAM的输入路径,构建是Unity Project中的) 我正在Minecraft中使用WorldEdit将构建保存到.schem文件中,然后使用Nuget中的NBTLIB将其保存到deleialze文件中,并且导出是.json文件。
问题: 尽管JSON文件具有一些恒定的属性,例如:“ Palettemax”,“ width”,“高度”和等,但也有一些动态值,例如块类型:“ Minecraft:Stone”,“ Minecraft”,“ Minecraft:Bedrock”。
问题: 知道某些值是动态的,一个人将如何访问它们并将它们存储在变量中以供团结使用(我知道这主要是AC#问题,因此此问题在stackoverflow上)
JSON文件的示例代码:
{
"PaletteMax": 2,
"Palette": {
"minecraft:stone": 0,
"minecraft:bedrock": 1
},
"Version": 2,
"Length": 2,
"Metadata": {
"WEOffsetX": -3,
"WEOffsetY": 0,
"WEOffsetZ": -2
},
"Height": 1,
"DataVersion": 2975,
"BlockData": [0, 1, 1, 0],
"BlockEntities": [],
"Width": 2,
"Offset": [1, -52, 2]
}
c#示例
private void deserialize()
{
using (var inputStream = File.OpenRead(input_path + file_name + ".schem"))
{
var nbtData = NbtConvert.ParseNbtStream(inputStream);
var dataObj = new RootNBT();
var tag = new NbtCompoundTag();
Debug.Log(nbtData.Count);
foreach(var key in nbtData.Keys)
{
switch (key)
{
case ("Length"):
INbtTag value;
nbtData.TryGetValue(key, out value);
dataObj.Length = int.Parse(value.ToJsonString());
Debug.Log(dataObj.Length); // this would output as 2 since the Length property in json above is 2
break;
case ("Palette"):
//this is the dynamic json part, can't access "minecraft:stone" without knowing its name
}
}
}
}
代码如果这里是 nbtlib 我用来对.schem文件的jonson将.sshem归档到json
Greetings my fellow stackers.
I working on re-creating a minecraft build in unity (yes I do know about mineways that it is possible to do so via .obj -> .fbx) Why I want to do such things is simply because I want all of the blocks to be individual and simple for user to interact with (just input path to saved build and ta-daaam the build is in the unity project)
I am using WorldEdit in minecraft to save the build into a .schem file, which is then deserialzed using NbtLib from nuget and the export is a .json file.
The problem:
though the json file has some constant properties such as: "PaletteMax", "Width", "Height" and etc., there are also some dynamic values such as the block type: "minecraft:stone", "minecraft:bedrock".
The question:
Knowing that some values are dynamic, how would one access them and store them in variables for use in unity (I know this is mostly a c# question hence this question is on stackoverflow)
Sample code of the json file:
{
"PaletteMax": 2,
"Palette": {
"minecraft:stone": 0,
"minecraft:bedrock": 1
},
"Version": 2,
"Length": 2,
"Metadata": {
"WEOffsetX": -3,
"WEOffsetY": 0,
"WEOffsetZ": -2
},
"Height": 1,
"DataVersion": 2975,
"BlockData": [0, 1, 1, 0],
"BlockEntities": [],
"Width": 2,
"Offset": [1, -52, 2]
}
Sample code from c#:
private void deserialize()
{
using (var inputStream = File.OpenRead(input_path + file_name + ".schem"))
{
var nbtData = NbtConvert.ParseNbtStream(inputStream);
var dataObj = new RootNBT();
var tag = new NbtCompoundTag();
Debug.Log(nbtData.Count);
foreach(var key in nbtData.Keys)
{
switch (key)
{
case ("Length"):
INbtTag value;
nbtData.TryGetValue(key, out value);
dataObj.Length = int.Parse(value.ToJsonString());
Debug.Log(dataObj.Length); // this would output as 2 since the Length property in json above is 2
break;
case ("Palette"):
//this is the dynamic json part, can't access "minecraft:stone" without knowing its name
}
}
}
}
Just in case here is the link to the NbtLib I was using to deserialize the .schem file to json
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设法找到解决方案!!
因此,基本上,我将嵌套的JSON视为一个单独的JSON,将其存储在变量中,并用根部组件进行了相同的操作。唯一的问题是,解析的流是NBTCompoundTag类型的类型,而Child Json的类型为InBttag,所以我只是将其扔到了NBTCompoundTag上,它起作用了!
^这条线解决了我遇到的问题。后来我把它当作词典,可以访问它的键
Managed to find a solution!!
So basically I treated the nested json as a separate json where I stored it in a variable and did the same thing i did with the root component. The only issue was that the parsed stream was of type NbtCompoundTag and the child json was of type INbtTag, so i just casted it to NbtCompoundTag and it worked!
^ this line solved the issue I had. Later i treated it like a dictionary where I can access keys of it