question

David Jones avatar image
David Jones asked

How to parse through title data in clouscript

CloudScriptTitle Data
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Jones avatar image David Jones commented ·

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
  }
0 Likes 0 ·
David Jones avatar image David Jones commented ·

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?

0 Likes 0 ·

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

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); 
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Jones avatar image David Jones commented ·

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.

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang David Jones commented ·
// 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);
}
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.