question

codastephen avatar image
codastephen asked

Cloud script response,Deserialization CloudScript result

HI everyone,

Im new here, and im a bit lost.

I'm calling an Azure function with unity, my function return :GetUserInventoryAsync(req);

When i'm checking the json, it look ok :

"{\"result\":{\"inventory\":[{\"annotation\":null,\"bundleContents\":null,\"bundleParent\":null,\"catalogVersion\":\"Item\",\"customData\":{\"Attack\":\"6\"},\"displayName\":\"My first item\",\"expiration\":null,\"itemClass\":\"Helmet\",\"itemId\":\"Helmet\",\"itemInstanceId\":\"EB4C568067D54323\",\"purchaseDate\":\"2021-11-16T12:51:03.629Z\",\"remainingUses\":null,\"unitCurrency\":null,\"unitPrice\":0,\"usesIncrementedBy\":null},{\"annotation\":null,\"bundleContents\":null,\"bundleParent\":null,\"catalogVersion\":\"Item\",\"customData\":null,\"displayName\":\"My first item\",\"expiration\":null,\"itemClass\":\"Helmet\",\"itemId\":\"Helmet\",\"itemInstanceId\":\"A38E8CDB6BC180D5\",\"purchaseDate\":\"2021-11-16T12:50:53.323Z\",\"remainingUses\":null,\"unitCurrency\":null,\"unitPrice\":0,\"usesIncrementedBy\":null}],\"playFabId\":\"E3FF6E6B12136B95\",\"virtualCurrency\":{\"GO\":50},\"virtualCurrencyRechargeTimes\":{}},\"customData\":null,\"error\":null}"

My question is : How can i work with these info ? What is the best way to do it ?

I tried to Deserialize the json into "GetUserInventoryResult", but the object is empty.

GetUserInventoryResult r = PlayFabSimpleJson.DeserializeObject<GetUserInventoryResult>(result.FunctionResult.ToString());

Thanks for your help :)

,

Hi everyone,

Im new here and im a bit lost.

Im calling an Azure function from Unity,

my Azure function is returning : GetUserInventoryAsync(request);and when i check the result.functionResult the json look ok :
"{\"result\":{\"inventory\":[{\"annotation\":null,\"bundleContents\":null,\"bundleParent\":null,\"catalogVersion\":\"Item\",\"customData\":{\"Attack\":\"6\"},\"displayName\":\"My first item\",\"expiration\":null,\"itemClass\":\"Helmet\",\"itemId\":\"Helmet\",\"itemInstanceId\":\"EB4C568067D54323\",\"purchaseDate\":\"2021-11-16T12:51:03.629Z\",\"remainingUses\":null,\"unitCurrency\":null,\"unitPrice\":0,\"usesIncrementedBy\":null},{\"annotation\":null,\"bundleContents\":null,\"bundleParent\":null,\"catalogVersion\":\"Item\",\"customData\":null,\"displayName\":\"My first item\",\"expiration\":null,\"itemClass\":\"Helmet\",\"itemId\":\"Helmet\",\"itemInstanceId\":\"A38E8CDB6BC180D5\",\"purchaseDate\":\"2021-11-16T12:50:53.323Z\",\"remainingUses\":null,\"unitCurrency\":null,\"unitPrice\":0,\"usesIncrementedBy\":null}],\"playFabId\":\"E3FF6E6B12136B95\",\"virtualCurrency\":{\"GO\":50},\"virtualCurrencyRechargeTimes\":{}},\"customData\":null,\"error\":null}"

What is the best way to work with these infos ?

I tried to Deserialize the json into "GetUserInventoryResult", but everything is empty.

GetUserInventoryResult r = PlayFabSimpleJson.DeserializeObject<GetUserInventoryResult>(result.FunctionResult.ToString());

Thanks for your help !

10 |1200

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

JayZuo avatar image
JayZuo answered

The problem here is that by default, the C# Azure Function will serializer the response with Camel Case. As you've shared in your json, you can find "Inventory" has been serialized as "inventory" and "VirtualCurrency" has been serialized to "virtualCurrency". Thus, in client, when you use

    GetUserInventoryResult r =PlayFabSimpleJson.DeserializeObject<GetUserInventoryResult>(result.FunctionResult.ToString());

    It gives you an empty object.

    To resolve this issue, you can return a string instead of the object like:

    var result = await PlayFabServerAPI.GetUserInventoryAsync(new PlayFab.ServerModels.GetUserInventoryRequest
    {
        PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId
    });
    if (result.Error == null)
    {
        return JsonConvert.SerializeObject(result.Result);
    }
    

    Or, if you are using isolated process functions in C#, you can set the JsonSerializerOptions to to leave property names unchanged.

    After this, you should be able to deserialize the result in client with no issue.

    10 |1200

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

    Sarah Zhang avatar image
    Sarah Zhang answered

    It looks like your development language is C#, and you used the method PlayFab.SimpleJson.DeserializeObject<YourType>(JSONString) to convert your JSON string to the object. The method you used to deserialize JSON string is correct. The issue should be related to the type you used.

    For clarification, our pre-defined class GetUserInventoryResult does not declare all the corresponding properties of the JSON string, you cannot use this type to deserialize the JSON string a valid object. If you want to define the class – GetUserInventoryResult on your own, it should be defined as something like this. Could you please check if you defined the class correctly?

    public class CustomData
    {
        /// <summary>
        /// 
        /// </summary>
        public string Attack { get; set; }
    }
     
    public class InventoryItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string annotation { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string bundleContents { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string bundleParent { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string catalogVersion { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public CustomData customData { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string displayName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string expiration { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string itemClass { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string itemId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string itemInstanceId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string purchaseDate { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string remainingUses { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string unitCurrency { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int unitPrice { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string usesIncrementedBy { get; set; }
    }
     
    public class VirtualCurrency
    {
        /// <summary>
        /// 
        /// </summary>
        public int GO { get; set; }
    }
     
    public class VirtualCurrencyRechargeTimes
    {
    }
     
    public class Result
    {
        /// <summary>
        /// 
        /// </summary>
        public List <InventoryItem > inventory { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string playFabId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public VirtualCurrency virtualCurrency { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public VirtualCurrencyRechargeTimes virtualCurrencyRechargeTimes { get; set; }
    }
     
    public class GetUserInventoryResult
    {
        /// <summary>
        /// 
        /// </summary>
        public Result result { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string customData { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string error { get; set; }
    }
    
    
    
    

    This is our test code that can output the inventory instances' display names correctly.

    using System;
    using System.Collections.Generic;
    using PlayFab.Json;
    
    
    namespace PlayFabTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                PlayFabTest.GetUserInventoryResult r = PlayFabSimpleJson.DeserializeObject<GetUserInventoryResult>("{\"result\":{\"inventory\":[{\"annotation\":null,\"bundleContents\":null,\"bundleParent\":null,\"catalogVersion\":\"Item\",\"customData\":{\"Attack\":\"6\"},\"displayName\":\"My first item\",\"expiration\":null,\"itemClass\":\"Helmet\",\"itemId\":\"Helmet\",\"itemInstanceId\":\"EB4C568067D54323\",\"purchaseDate\":\"2021-11-16T12:51:03.629Z\",\"remainingUses\":null,\"unitCurrency\":null,\"unitPrice\":0,\"usesIncrementedBy\":null},{\"annotation\":null,\"bundleContents\":null,\"bundleParent\":null,\"catalogVersion\":\"Item\",\"customData\":null,\"displayName\":\"My first item\",\"expiration\":null,\"itemClass\":\"Helmet\",\"itemId\":\"Helmet\",\"itemInstanceId\":\"A38E8CDB6BC180D5\",\"purchaseDate\":\"2021-11-16T12:50:53.323Z\",\"remainingUses\":null,\"unitCurrency\":null,\"unitPrice\":0,\"usesIncrementedBy\":null}],\"playFabId\":\"E3FF6E6B12136B95\",\"virtualCurrency\":{\"GO\":50},\"virtualCurrencyRechargeTimes\":{}},\"customData\":null,\"error\":null}");
                Result result= r.result;
                var Inventory = result.inventory;
                foreach (var item in Inventory)
                {
                    Console.WriteLine(item.displayName);
                }
            }
        }
    
    
        public class CustomData
        {
            /// <summary>
            /// 
            /// </summary>
            public string Attack { get; set; }
        }
    
    
        public class InventoryItem
        {
            /// <summary>
            /// 
            /// </summary>
            public string annotation { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string bundleContents { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string bundleParent { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string catalogVersion { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public CustomData customData { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string displayName { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string expiration { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string itemClass { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string itemId { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string itemInstanceId { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string purchaseDate { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string remainingUses { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string unitCurrency { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int unitPrice { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string usesIncrementedBy { get; set; }
        }
    
    
        public class VirtualCurrency
        {
            /// <summary>
            /// 
            /// </summary>
            public int GO { get; set; }
        }
    
    
        public class VirtualCurrencyRechargeTimes
        {
        }
    
    
        public class Result
        {
            /// <summary>
            /// 
            /// </summary>
            public List<InventoryItem> inventory { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string playFabId { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public VirtualCurrency virtualCurrency { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public VirtualCurrencyRechargeTimes virtualCurrencyRechargeTimes { get; set; }
        }
    
    
        public class GetUserInventoryResult
        {
            /// <summary>
            /// 
            /// </summary>
            public Result result { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string customData { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string error { get; set; }
        }
    
    
    
    
    }
    
    
    
    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.