question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

How to convert a group data from Cloudscript easily?

Hello everyone,

I'm looking for a way to parse the result from the Cloudscript to a C# class easily. The data I return might contain many keys. In that case, the current way I do is not efficient from my point of view.

Here's the simplified version of how I'm doing this:

Cloudscript:

handlers.GetData = function(args, context) {
    var data1;
    var data2;
    var data3;
    var data4;
    var data5;
    
    //... some functions to get data. The data are not related to each other

    return {
        "Data1": data1,
        "Data2": data2,
        "Data3": data3,
        "Data4": data4,
        "Data5": data5
    }
}

C#:

private class DataClass
{
    public long Data1;
    public long Data2;
    public string Data3;
    public int Data4;
    public long Data5;
}

private DataClass MyDataClass = new DataClass();



    private void OnGetDataSuccess(ExecuteCloudScriptResult result)
    {
        JsonObject jsonResult = (JsonObject)result.FunctionResult;

        if (jsonResult.TryGetValue("Data1", out object data1))
        {
            MyDataClass.Data1 = long.Parse(data1.ToString());
        }
        if (jsonResult.TryGetValue("Data2", out object data2))
        {
            MyDataClass.Data2 = long.Parse(data2.ToString());
        }
        if (jsonResult.TryGetValue("Data3", out object data3))
        {
            MyDataClass.Data3 = data3.ToString();
        }
        if (jsonResult.TryGetValue("Data4", out object data4))
        {
            MyDataClass.Data4 = int.Parse(data4.ToString());
        }
        if (jsonResult.TryGetValue("Data5", out object data5))
        {
            MyDataClass.Data5 = long.Parse(data5.ToString());
        }
    }
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

You can use JSON.NET to deserialize the object returned from CloudScript to your own C# class: https://www.newtonsoft.com/json/help/html/DeserializeObject.htm#!

Here is the sample code you may find helpful:

using PlayFab;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PlayFab.ServerModels;
using Newtonsoft.Json;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {


            PlayFab.PlayFabSettings.staticSettings.DeveloperSecretKey = "xxxxxxxxxx";
            PlayFab.PlayFabSettings.staticSettings.TitleId = "xxxx";
            Task<PlayFabResult<PlayFab.ServerModels.ExecuteCloudScriptResult>> t = PlayFabServerAPI.ExecuteCloudScriptAsync(
                new ExecuteCloudScriptServerRequest()
                {
                    PlayFabId = "xxxx",
                    FunctionName = "test",
                }
     
                );
            t.Wait();
            DataClass data = JsonConvert.DeserializeObject<DataClass>(JsonConvert.SerializeObject(t.Result.Result.FunctionResult));
            Console.ReadKey();
        }
    }


    public class DataClass
    {
        public long Data1;
        public long Data2;
        public string Data3;
        public int Data4;
        public long Data5;
    }
}



CloudScript:

handlers.test = function (args, context){
  return {
      "Data1": 1,
      "Data2": 2,
      "Data3": 3,
      "Data4": 4,
      "Data5": 5
  }
}
1 comment
10 |1200

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

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

Thank you for the answer

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.