question

MoonHeonYoung avatar image
MoonHeonYoung asked

how can i access json value??

In C # code,

I want to know how to access a deeper json key.

I want to access the loginstreak key. Is my code above correct?

result.Data ["CheckInTracker"].Value

I know that this is the first approach.

Because that value is a string so The parse process seems to be necessary. Is that right??

How to access deep json key with parses variable after that?

acess.png (26.4 KiB)
accccc.png (25.9 KiB)
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

@kesyas>> 1. ...can I solve it with an if trygetvalue () nested statement?

After you deserialize them, yes.

>> 2. How can I get the int value as shown in the screenshot?

You can use Convert.ToInt32 to convert the object value to int type.

>> 3. ... Playfab.PfEditor.SimpleJson are no longer used. Is that right??

It’s not recommended but you can use it if you want.

If so, is the only way to deal with json data received from Playfab is to use PlayFab.PluginManager.GetPlugin ....(testUserDataRecord.Value)?

No, it’s not the only, it’s just an encapsulation of such JSON parsing methods. You can also use other external JSON parsing methods if you want.

>> -1 Is the data that can be sent from Playfab Cloud Code to the client a JSON string or a JSON object?

It would be the Object.

>> -2 Are all result.Data values generated by API calls from clients dictionary?

The JSONObject that defined by PlayFab is the implementation of the Dictionary which owns the methods of the dictionary objects.

You can check the SDK code on your own by the “Go to definition” option in Visual Studio. More advanced questions about Unity and C# programming, you can navigate to the corresponding forum for professional supports.

More technical questions of PlayFab, we would suggest submitting new questions for them.

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.

MoonHeonYoung avatar image MoonHeonYoung commented ·

thanks : ) i understand it

0 Likes 0 ·
Sarah Zhang avatar image
Sarah Zhang answered

You can uncomment a line to make SimpleJson class Internal when you need it. It would be better to use PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject(testUserDataRecord.Value) to convert a string to an object which can be converted to a JsonObject. You can refer to the following code.

using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.Json;
...


    public void GetUserReadOnlyData()
{
    UserDataRecord testUserDataRecord;


    PlayFabClientAPI.GetUserReadOnlyData(new PlayFab.ClientModels.GetUserDataRequest
    {
        Keys = new List<string> { "CheckInTracker" }
    }, result =>
    {


        if (result.Data.TryGetValue("CheckInTracker", out testUserDataRecord))
        {
            JsonObject testJsonObj = (JsonObject)PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject(testUserDataRecord.Value);
            object testJsonValue;
            if (testJsonObj.TryGetValue("LoginStreak", out testJsonValue))
            {
                Debug.Log(testJsonValue.ToString());
            }
        }
    }, error => { });
}
...
10 |1200

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

MoonHeonYoung avatar image
MoonHeonYoung answered

Thank you for answer.

1. Then, if there are deeper json keys, can I solve it with an if trygetvalue () nested statement?

2. Question about the object variable obtained by out keyword.

It seems to have to be cast.

How can I get the int value as shown in the screenshot?

My code gives an error like screenshot.

3. Features like Playfab.PfEditor.SimpleJson are no longer used.

Is that right??

An error occurs when building.

If so, is the only way to deal with json data received from Playfab is to use PlayFab.PluginManager.GetPlugin <ISerializerPlugin> (PluginContract.PlayFab_Serializer) .DeserializeObject (testUserDataRecord.Value)?

4. I have a lot of questions because I don't know much about programming.

I know that JSON is used for communication between Playfab and client.

Here are two questions.

-1 Is the data that can be sent from Playfab Cloud Code to the client a JSON string or a JSON object? (I.e. whether it is a JSON object stringified through JSON.stringify or the JSON object itself)

The reason for the above question is,

in the cloudcode function,

difference between return JSON.stringify (grantedItems); and return {messageValue: message};

I want to know the difference between the above two return values.

-2 Are all result.Data values generated by API calls from clients dictionary?

The tryGetValue function is known as the Dictionary function.

Thank you!


pafaf.png (128.9 KiB)
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.