Sign In Register

How can we help you today?

Start a new topic

Getting JSON information from a GameSparks Properties object (Unity)

I currently have a "Property" in gamesparks that looks like this:


{
    "newPreds": [
        {
            "question": "Who will win?",
            "optionOne": "Bob",
            "optionTwo": "Jim"
        },
        {
            "question": "Who will win?",
            "optionOne": "Tom",
            "optionTwo": "Joe"
        }
    ]
}

  However I'm really struggling to figure out how I get at the data that's in any of these fields (quesion, optionOne, optionTwo) from within my application.


If I simplify things and make my gamesparks property just this: 

 

{
            "question": "Who will win?",
            "optionOne": "Bob",
            "optionTwo": "Jim"
}

 I can get this information just fine, by using the Unity JSONUtility. I have an Object Class called QuestionObject that has three fields, (question, optionOne, optionTwo) so I can use the following code to easily convert the JSON data received from the GameSparks property object into my own "QuestionObject"

QuestionObject myQuestion = JsonUtility.FromJson<QuestionObject>(<My GameSparks Property Here>.JSON);

 However I'd like to be able to pass in any number of Question objects from the server to the application at the same time, so I'd prefer to have it formatted in Gamesparks like it is in my first code chunk, but when it's passed in with the "newPreds" wrapper around it, and multiple JSON's I can't figure out how to get at them.


  Something like this should work for you:

new GetPropertyRequest().SetPropertyShortCode("yourShortCode").Send(response => {
	if(!response.HasErrors)
	{
		var newPreds = response.ScriptData.GetGSDataList("newPreds");

		for(int i = 0; i < newPreds.Count; i++)
		{
			QuestionObject myQuestion = JsonUtility.FromJson<QuestionObject>(newPreds[i].JSON);
		}
	}
});

  


1 person likes this

Ah sorry, my bad.

Replace this:

 

var newPreds = response.ScriptData.GetGSDataList("newPreds");

 With this:

 

var newPreds = response.Property.GetGSDataList("newPreds");

 


1 person likes this

Thanks, I'll try that out instead.



My gamesparks property looks like this:


{

    "propositions": [

        [

            "Who will win?",

            "Bob",

            "Joe"

        ],

        [

            "Who will win?",

            "Jim",

            "John"

        ]

    ]

}

, however when I do a getPropertyRequest on the property name and then use response.Property.GetGSDataList("propositions"); my GSDataList().Count is always 0.

Hi David,

 

Thanks for the help, that looks like it should work, but I'm getting an error because newPreds is always Null.


I'm assuming I need to add something to the cloud code of my GetPropertyRequest to set the ScriptData?


Login to post a comment