I accidentally clicked login and post before I actually wrote my question but i was hoping somebody could explain how to parse through my JSON files in cloudscript. I'm used to coding in C# in unity so javascript is a little foreign to me.
Lets say this was my title data could someone give me a way to pull out the ratingTop value so I can then compare it to another int?
"Settler": { "id": 1, "rating": 0, "ratingTop": 49 }, "Rebellious": { "id": 2, "rating": 50, "ratingTop": 99 }
A second question is how would I get the next key once they pass the "ratingTop"? For example thier rating is now 50 so they move on to the next Class which would be Rebellious. I understand how I could hardcode the name to save to playerdata(title) but there is going to be more than just 2 "levels" so is there a way to iterate to the next key in title data result?
Please refer to the following code.
var jsonString = {"Settler": { "id": 1, "rating": 0, "ratingTop": 49 }, "Rebellious": { "id": 2, "rating": 50, "ratingTop": 99 }}; // Parse var jsonObject = JSON.parse(jsonString); //to pull out the ratingTop value var ratingTop = jsonObject.ratingTop; //to iterate to the next keyjsonObject.id = 3; jsonObject.rating = 70; jsonObject.ratingTop = 150; var newJsonString = JSON.stringify(jsonObject);
How would you differentiate the "settler" class from the "vigilante" class? I see how you pulled out ratingTop but should there be an extra line above that one? Im trying to check the current "class" of the player and see if they move up or not each time "rating" is awarded in game. But besides that this helped me pretty much build my function in cloudscript, thank you.
// your json string var jsonString = '{"Settler":{"id":1,"rating":0,"ratingTop":49},"Rebellious":{"id":2,"rating":50,"ratingTop":99}}'; // pares it into a json object var jsonObj = JSON.parse(jsonString); // get the value as a normal object var ratingTop1 = jsonObj.Settler.ratingTop; var ratingTop2 = jsonObj.Rebellious.ratingTop; // iterate over the json object // the 'key' is the property of the jsonObject for(var key in jsonObj){ console.log(key); // get the 'ratingTop' for each key console.log(jsonObj[key].ratingTop); }
2 People are following this question.