So my JSON format is like:
{
"1" : {
"username" : "fdsfds",
"timeToComplete" : 3.34779953956604,
"playerLevel" : 1,
"armourLevel" : 1,
"weaponLevel" : 1,
"totalMoney" : 0,
"enemiesKilled" : 0,
"gameCompleted" : true,
"score" : 33
},
"2" : {
"username" : "trgokt",
"timeToComplete" : 5.32478567436,
"playerLevel" : 1,
"armourLevel" : 1,
"weaponLevel" : 1,
"totalMoney" : 0,
"enemiesKilled" : 0,
"gameCompleted" : true,
"score" : 19
}
}
And I want to increment a counter for every time the parent number occurs (so like 1 or 2 in this case). I don’t know how you would go about doing this so can anyone give me some pointers?
Thanks!
At the moment I don’t have much time. However you use an object like an array which makes no sense whatsoever. Why don’t you use an array in the first place? The point of an array is to have an ordered collection of “values” / objects
[
{
"username" : "fdsfds",
"timeToComplete" : 3.34779953956604,
"playerLevel" : 1,
"armourLevel" : 1,
"weaponLevel" : 1,
"totalMoney" : 0,
"enemiesKilled" : 0,
"gameCompleted" : true,
"score" : 33
},
{
"username" : "trgokt",
"timeToComplete" : 5.32478567436,
"playerLevel" : 1,
"armourLevel" : 1,
"weaponLevel" : 1,
"totalMoney" : 0,
"enemiesKilled" : 0,
"gameCompleted" : true,
"score" : 19
}
]
That’s just an array with two elements with the indices 0 and 1.
I guess you’re still struggling with your original question? You slowly added some details in various comments over time which was missing in your actual question. Your original code would actually almost work if you want to use Unity’s JsonUtility. However instead of reading / writing “statData” you should read and write only your “stats” object. When you want to add elements to your list you have to do a read - modify - write operation. So you first read your stats object from your saved json text. Then you can simply add / remove / modify the List inside the stats object as you wish. When done just save the stats object back and overwrite the old json text.