2

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:

        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:

{
'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)

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 Answer 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.

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:

{
    "Game": [
        {
            "Room": "Vault111FreezeChamber",
            "Attributes": {
                "Description": "Thefreezechamberofthevaultyouenteredafterthenuclearfallout.",
                "Exits": "North.Vault111: MainHallway"
            },
            "Room": "Vault111MainHallway",
            "Attributes": {
                "Description": "Themainhallwayofthevault.",
                "Exits": "South.Vault111: FreezeChamber"
            }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.