I’m trying to parse in some data from a json file to create a list of objects in unity. The first object in the json array parses just fine, but the second one gives me an Argument is out of range error. I can’t figure out why this is happening.
JSON file:
[
{
"characterName": "name",
"characterAvatar": "CharacterSprites/",
"characterID": 0
},
{
"characterName": "name2",
"characterAvatar": "CharacterSprites/",
"characterID": 2
}
]
void Start() {
characterData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Character Database.json"));
ConstructCharacterDatabase();
if (characters.Count != 0) {
print(characters[1].characterName);
}
}
void ConstructCharacterDatabase() {
for (int i = 0; i < characterData.Count; i++) {
//characters.Add(new character(characterData[i]["characterName"].ToString(), characterData[i]["characterAvatar"].ToString(), (int)storyOrderData[i]["characterID"]));
Debug.Log(characterData[i]["characterName"].ToString() + " " + characterData[i]["characterAvatar"].ToString() + " " + (int)storyOrderData[i]["characterID"]);
}
}
I’v commented out the line that adds new objects to my list. I write a debug.log line for testing. The first object in my json file is printed, on the second loop of the for loop it gives an error.