从JSON加载多个“节点”并存储到数组中

时间:2022-07-21 21:17:05

I am currently creating a small Text-Based Game. In this there are obviously multiple rooms, I wish to load those rooms from a JSON file. I am currently doing that as such:

我目前正在创建一个基于文本的小游戏。这里显然有多个房间,我希望从JSON文件中加载这些房间。我目前正在这样做:

        dynamic jRooms = Json.Decode(file);
        for (int i = 0; i < Regex.Matches( file,  "Room" ).Count; i++){
            name[i] = jRooms.Game.Room[i];
            description[i] = jRooms.Game.Room.Attributes.Description[i];
            exits[i] = jRooms.Game.Room.Attributes.Exits[i];
            _count++;
        }

That loads information from the following JSON file:

从以下JSON文件加载信息:

{
'Game': [{
    'Room': 'Vault 111 Freeze Chamber',
        'Attributes': { 
            'Description': 'The freeze chamber of the vault you entered after the nuclear fallout.',
            'Exits': 'North.Vault 111: Main Hallway'
        },
    'Room': 'Vault 111 Main Hallway',
        'Attributes': { 
            'Description': 'The main hallway of the vault.',
            'Exits': 'South.Vault 111: Freeze Chamber'
        }
    }]}

This unfortunately throws up an error during run time that I can't seem to work out, which is the following:

不幸的是,这在运行时抛出了一个我似乎无法解决的错误,如下所示:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference at CallSite.Target(Closure , CallSite , Object , Int32 ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at TBA.Loader.Rooms() at TBA.Program.Main(String[] args)

Microsoft.CSharp.RuntimeBinder。RuntimeBinderException:不能在CallSite的空引用上执行运行时绑定。目标(关闭,CallSite, Object, Int32)在system . dynamic.updatedelegate。UpdateAndExecute2[T0,T1,TRet](调用站点,T0 arg0, T1 arg1)位于TBA.Loader.Rooms()位于TBA.Program。Main(String[]args)

Any help would be greatly appreciated, because I am completely stumped as to what is wrong and not working. If you need anymore of my code, just request it.

任何帮助都将非常感谢,因为我完全被什么是错误和不工作所困扰。如果你需要我的代码,就请求它。

Thanks.

谢谢。

1 个解决方案

#1


1  

The problem is with your JSON. JSON doesn't allow single quotes (maybe they have a different meaning or no meaning at all). Source - W3Schools.

问题出在JSON上。JSON不允许单引号(也许它们有不同的含义,或者根本没有意义)。——W3Schools来源。

Use services like JSONLint to validate JSON and check for errors. Even JSONLint declares your JSON, invalid. Using double quotes however, it is declared valid. You should use double quotes like this:

使用JSONLint这样的服务来验证JSON并检查错误。甚至JSONLint也声明了JSON无效。但是使用双引号,它被声明为有效的。你应该使用这样的双引号:

{
    "Game": [
        {
            "Room": "Vault111FreezeChamber",
            "Attributes": {
                "Description": "Thefreezechamberofthevaultyouenteredafterthenuclearfallout.",
                "Exits": "North.Vault111: MainHallway"
            },
            "Room": "Vault111MainHallway",
            "Attributes": {
                "Description": "Themainhallwayofthevault.",
                "Exits": "South.Vault111: FreezeChamber"
            }
        }
    ]
}

#1


1  

The problem is with your JSON. JSON doesn't allow single quotes (maybe they have a different meaning or no meaning at all). Source - W3Schools.

问题出在JSON上。JSON不允许单引号(也许它们有不同的含义,或者根本没有意义)。——W3Schools来源。

Use services like JSONLint to validate JSON and check for errors. Even JSONLint declares your JSON, invalid. Using double quotes however, it is declared valid. You should use double quotes like this:

使用JSONLint这样的服务来验证JSON并检查错误。甚至JSONLint也声明了JSON无效。但是使用双引号,它被声明为有效的。你应该使用这样的双引号:

{
    "Game": [
        {
            "Room": "Vault111FreezeChamber",
            "Attributes": {
                "Description": "Thefreezechamberofthevaultyouenteredafterthenuclearfallout.",
                "Exits": "North.Vault111: MainHallway"
            },
            "Room": "Vault111MainHallway",
            "Attributes": {
                "Description": "Themainhallwayofthevault.",
                "Exits": "South.Vault111: FreezeChamber"
            }
        }
    ]
}