question

marcmptest avatar image
marcmptest asked

Retrieving PlayerData as a json

Hello, pardon me as I am rather new to this system and unity in general, may I know how do I retrieve PlayerData in the form of json ? I am rather unsure of the syntax of GetUserData, ideally my goal is to retrieve the result of all player data and then process it client side.

For example, I have many of the following records :
"questionno": 1, "questiondifficulty": 0, "correctsquares": 4, "wrongsquares": 5, "questionaccuracy": 0, "questionelapsedtime": 9.71 stored as a JSON in playerdata, my goal is to iterate through the result and pick out records that have a question difficulty of 0, may I know what would be the best solution for this problem?

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

·
Sarah Zhang avatar image
Sarah Zhang answered

GetUserData API only supports using specific keys to get the corresponding K/V pairs of the specific Player. So if you want to get multiple K/V pairs of one player, you need to enter all these keys as a C# list in the Unity scripts. Suppose that keys are “A”,”B”,”C”, you can refer to the following code to pick out records that have a question difficulty of 0.

public void OnGetUserDataClick()
    {
        PlayFabClientAPI.GetUserData(new GetUserDataRequest
        {
            Keys = new List<string> { "A", "B", "C" },
        },
        this.OnGetUserDataSuccess,
        this.OnError);
    }


    private void OnGetUserDataSuccess(GetUserDataResult result)
    {
        foreach (var item in result.Data)
        {


            JsonObject value = (JsonObject)PlayFabSimpleJson.DeserializeObject(item.Value.Value);


            foreach (var attribute in value)
            {
                if (attribute.Key == "questiondifficulty")
                {
                    //Debug.Log(attribute.Value);


                    if (attribute.Value.ToString() == "0")
                    {
                        Debug.Log(item.Key);
                        Debug.Log(item.Value.Value);


                    }
                }
            }
        }
    }
    public void OnError(PlayFabError error)
    {
        Debug.Log(error.GenerateErrorReport());
    }


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.