加载多个';节点';从JSON中提取并存储到数组中

本文关键字:提取 存储 数组 节点 加载 JSON | 更新日期: 2023-09-27 18:24:45

我目前正在创建一个小型的基于文本的游戏。这里显然有多个房间,我希望从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++;
        }

它从以下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'
        }
    }]}

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

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法对null引用执行运行时绑定在CallSite.Target(闭包、CallSite、Object、Int32)在System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite站点,T0 arg0,T1 arg1)在TBA.Loader.Rooms()在TBA.Program.Main(String[]args)

任何帮助都将不胜感激,因为我完全不知道哪里出了问题,而且不起作用。如果你还需要我的代码,就请求吧。

谢谢。

加载多个';节点';从JSON中提取并存储到数组中

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

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

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