question

Kim Strasser avatar image
Kim Strasser asked

How can I read json Title Player Data in Cloud script?

I have created this key/value pair in Cloud script:

Afterwards, I need to get the values of "UTCDate", "UTCMonth" and "UTCYear again in my Cloud script function, but I don't know how to read the values in Cloud script.

For example, previous date is undefined, but it should be 1, because "UTCDate":1 in Title Player Data.

"Logs": [
{
    "Level": "Info",
    "Message": "previous date: undefined",
    "Data": null
}
],
var previousUTCyear;
var previousUTCmonth;
var previousUTCdate;

var resultdata = server.GetUserReadOnlyData({PlayFabId: currentPlayerId, Keys: "LastDatePlayed"});
if ((resultdata.Data.hasOwnProperty("LastDatePlayed")) && (resultdata.Data.LastDatePlayed.Value != "") && (resultdata.Data.LastDatePlayed.Value != null))
{
    var jsondata = resultdata.Data.LastDatePlayed.Value;
    previousUTCdate = jsondata.UTCDate;
}

What is wrong with my Cloud script code? How can I get the value of "UTCDate"?

CloudScript
10 |1200

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

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

resultdata.Data.LastDatePlayed.Value is a Json String, you first need to parse it in order to access its properties, please change line 9 to the following code:

  previousUTCdate = JSON.parse(jsondata).UTCDate;
10 |1200

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

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.