question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

How to convert string to JsonObject?

Hello everyone,

I need to convert a JSON string, which is taken from Title Data, to a PlayFab's JsonObject, but when I convert the string to JsonObject, I get invalid cast error. How can I do this conversion?

I can't use the Unity's JsonUtility class because the JSON is dynamic. The keys of the JSON changes. I need a JsonObject to check if the key I'm looking for exists and get its value.

10 |1200

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

Citrus Yan avatar image
Citrus Yan answered

@Ozan Yilmaz

For instance, if you want to access "Min", simply do the following:

string json = result.Data["Versions"];
dynamic obj = PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject(json);
Debug.Log("Min:" + obj["Min"]);

Where "Versions" is in this format:

{  "Min": "1.0.5",  "1.1.0": 495,  "1.0.9": 490,  "1.0.8": 485,  "Other": 484}
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 solution. It works. Also, is there a way to check if a desired key exist in that "obj" variable? Currently, I'm using try-catch blocks to check if a desired key exists.

0 Likes 0 ·
Citrus Yan avatar image
Citrus Yan answered

Could you please share some of your code snippet & the rough JSON structure so that we can better understand your question?

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 answered

@Citrus Yan

This is the Title Data:

{
  "Min": "1.0.5",
  "1.1.0": 495,
  "1.0.9": 490,
  "1.0.8": 485,
  "Other": 484
}

I basically want to get a value depending on the versions the players playing with.

For example; let's say there are 3 players (A, B and C) playing the game with the versions of 1.1.0, 1.0.7 and 1.0.4 respectively.

When the player A logs into their account, I'll try to read a key of "1.1.0". If I find the key in the JSON file, I'll read that value and will change some configurations in the game. In this case, the player A will read the value of 495.

When the player B logs into their account, I'll try to read a key of "1.0.7". In this case, the key won't be found, and the game will read the "Min" key to check if the version is high enough to play the game, which is high enough. Since there's no key of "1.0.7", the game will read the "Other" key to configure the game.

When the player C logs into their account, the game will try to check if a key of "1.0.4" exists. In this case, it won't find the key. Then it will look for the "Min" key. Since the version is lower than the minimum version (1.0.5), I will show a message saying something about updates.

The JSON file will change as I update the game. For example; in the next version the JSON can be something like this:

{
  "Min": "1.0.9",
  "1.1.1": 500,
  "1.1.0": 495,
  "Other": 490
}

I use Unity for the game. So that Title Data is a dictionary consisting of string-string key-value pairs. I normally use JsonUtility class, which is one of Unity's classes, but it only works if you know the JSON file's keys beforehand. In this case, the keys are dynamic. I change them as I update the game.

On the other hand, JsonObject, which is a class in the PlayFab.Json library, has a fuction to check if a desired key exists in the JSON. I need to use that ability to do what I want. In Unity, Title Data is read as a string. So, I need to conver it to JsonObject.

    private void OnLogin(LoginResult result)
    {
        string versionCheck = result.InfoResultPayload.TitleData["Version"];
        // How can I convert 'versionCheck' to JsonObject?
    }
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.