如何访问嵌套的JSON中的动态嵌套JSON(我不知道预定的值和键)

发布于 2025-01-18 00:04:23 字数 1964 浏览 1 评论 0原文

问候我的堆放者。 我致力于重新创建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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2025-01-25 00:04:23

设法找到解决方案!!

因此,基本上,我将嵌套的JSON视为一个单独的JSON,将其存储在变量中,并用根部组件进行了相同的操作。唯一的问题是,解析的流是NBTCompoundTag类型的类型,而Child Json的类型为InBttag,所以我只是将其扔到了NBTCompoundTag上,它起作用了!

var value2 = (NbtCompoundTag)value1;

^这条线解决了我遇到的问题。后来我把它当作词典,可以访问它的键

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);
                        break;
                    case ("Palette"):
                        INbtTag value1;
                        nbtData.TryGetValue(key, out value1);
                        var value2 = (NbtCompoundTag)value1;
                        foreach(var paletteKey in value2.Keys)
                        {
                            Debug.Log(paletteKey);
                            INbtTag keyValue;
                            value2.TryGetValue(paletteKey, out keyValue);
                        }
                        break;
                }
            }
        }
    }

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!

var value2 = (NbtCompoundTag)value1;

^ this line solved the issue I had. Later i treated it like a dictionary where I can access keys of it

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);
                        break;
                    case ("Palette"):
                        INbtTag value1;
                        nbtData.TryGetValue(key, out value1);
                        var value2 = (NbtCompoundTag)value1;
                        foreach(var paletteKey in value2.Keys)
                        {
                            Debug.Log(paletteKey);
                            INbtTag keyValue;
                            value2.TryGetValue(paletteKey, out keyValue);
                        }
                        break;
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文