question

Kim Strasser avatar image
Kim Strasser asked

How can I use UpdateUserData to create json player data?

How can I create json data in a Player Data key/value pair?

My cloud script code is not working because I get this result:

"key :PushNotification1 json: [object Object]"

What is wrong with my cloud script code? How can I create the json value?

var notificationnumber = 1;
var key = "PushNotification" + notificationnumber.toString();

var currentdate = "22/02/2020";
var value = {"Timestamp":currentdate,"TitleEnglish":"You won this item","English":"The fantastic Bamboo Sword!","TitleFrench":"Tu as gagné cet objet","French":" L'Epée Bambou fantastique!","TitleGerman":"Du hast diesen Gegenstand gewonnen","German":"Das fantastische Bambus Schwert!"}

log.info("key :" + key + " json: " + valuejson);
var obj = {};
obj[key] = valuejson
var result = server.UpdateUserData({ PlayFabId: currentPlayerId, Data: obj, Permission: UserDataPermission.Private });

In addition, how can I create a json value in the client code when I use API UpdateUserData?

My code is not working.

var currentdate = "22/02/2020";
var result = await PlayFabClientAPI.UpdateUserDataAsync(new PlayFab.ClientModels.UpdateUserDataRequest()
{
    Data = new Dictionary<string, string>() {
    {"Playerdatakey", "{"Timestamp":currentdate,"Name":"Tommy","Haircolour":"Brown","Eyecolour":"Blue"};"}
}
});
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

May I know where did the variable valuejson come from because I cannot see it declared anywhere in your code. And, the way I think it, you probably want to do the following instead:

var notificationnumber = 1;
var key = "PushNotification" + notificationnumber.toString();


var currentdate = "22/02/2020";
var value = {"Timestamp":currentdate,"TitleEnglish":"You won this item","English":"The fantastic Bamboo Sword!","TitleFrench":"Tu as gagné cet objet","French":" L'Epée Bambou fantastique!","TitleGerman":"Du hast diesen Gegenstand gewonnen","German":"Das fantastische Bambus Schwert!"}


//you must stringify the json object into string first.


var valuejson = JSON.stringify(value);


log.info("key :" + key + " json: " + valuejson);
var obj = {};
obj[key] = valuejson;
var result = server.UpdateUserData({ PlayFabId: currentPlayerId, Data: obj, Permission: UserDataPermission.Private });


And, for the client code, using JsonConvert.SerializeObject to serialize a object is quite clear and efficient, please refer to the following code:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PlayFab;
using Newtonsoft.Json; //used for json de/seriailization
namespace TestProj
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var currentdate = "22/02/2020";
            PlayerDataValue value = new PlayerDataValue();
            value.Timestamp = currentdate;
            value.Name = "Tommy";
            value.Haircolour = "Brown";
            value.Eyecolour = "Blue";
            string jsonValue = JsonConvert.SerializeObject(value);
            var result = await PlayFabClientAPI.UpdateUserDataAsync(new PlayFab.ClientModels.UpdateUserDataRequest()
            {
                Data = new Dictionary<string, string>{
                    { "Playerdatakey", jsonValue }
            }
            });
            Console.ReadKey();
        }


    }
    public class PlayerDataValue
    {
        public string Timestamp { get; set; }
        public string Name { get; set; }
        public string Haircolour { get; set; }
        public string Eyecolour { get; set; }


    }
}



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.

Kim Strasser avatar image Kim Strasser commented ·

Thanx. It's exactly what I wanted to do.

But I have another question about the client code. Is it possible to do something like this in the client code when I use UpdateUserData?

This code is not working and I don't know how to do it right.

string key = "Playerdatakey";
var obj = {};
obj[key] = jsonValue;
    

var result = await PlayFabClientAPI.UpdateUserDataAsync(new PlayFab.ClientModels.UpdateUserDataRequest() { Data = new Dictionary<string, string>{ { obj } }

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

Please refer to the second code snippet in my answer, which does the same thing as in CloudScript.

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.