question

el-inside avatar image
el-inside asked

Convert Title Data (Json) to different Class Objects

Hello,
Im trying to retrieve the data from Title Data and store it into an Object.
As key i have Enemies and the Values are:

{
  "enemies": [
    {
      "id": 1,
      "name": "Pete",
      "hp": 50,
      "dmg": 5
    },
    {
      "id": 2,
      "name": "Jamie",
      "hp": 75,
      "dmg": 7
    },
    {
      "id": 3,
      "name": "Paul",
      "hp": 100,
      "dmg": 10
    }
  ]
}

Now im trying to create Objects from my Class Enemy

    [Serializable]
    public class Enemy
    {
        public string name;
        public int id;
        public int hp;
        public int dmg;

    }

And here is my code:

    //Get Enemies Stats
    public void get_enemystats()
    {
        var request = new GetTitleDataRequest();
        PlayFabClientAPI.GetTitleData(request, onEnemyResult, onEnemyError);
    }

    private void onEnemyError(PlayFabError obj)
    {
        Debug.Log("Failed to get Enemy stats");
    }

    public void onEnemyResult(GetTitleDataResult obj)
    {
        Debug.Log("Getting enemies Stats");
        Debug.Log(obj.Data.Values.ToString());



        Enemy enemynow = JsonUtility.FromJson<Enemy>(obj.Data.Values.ToString());
        
        Debug.Log(enemynow.name);
        //Debug.Log(enemynow.dmg);


    }

Do i have to define, which key i want to retrieve? For now i have only this one.

I keep getting errors on the Json parsing line, can someone please help me, what is wrong?

Maybe its the obj.Data.Values is a Dictionary?

Title Data
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.

JayZuo avatar image JayZuo ♦ commented ·

How did you stroe the "Enemies" in Title Data? You should specify the key in "GetTitleDataRequest". Could you share more details about your Title Data setting?

0 Likes 0 ·

1 Answer

·
JayZuo avatar image
JayZuo answered

I'm not sure how you store the "Enemies" in Title Data. But usually, it would be something like this:


And then you can get it in Client like the following:

//Get Enemies Stats
public void get_enemystats()
{
 var request = new GetTitleDataRequest { Keys = new List<string> { "Enemies" } };
 PlayFabClientAPI.GetTitleData(request, onEnemyResult, onEnemyError);
}

private void onEnemyError(PlayFabError obj)
{
 Debug.Log("Failed to get Enemy stats");
}

public void onEnemyResult(GetTitleDataResult obj)
{
 Debug.Log("Getting enemies Stats");
 Debug.Log(obj.Data["Enemies"]);

 var Enemies = PlayFab.Json.JsonWrapper.DeserializeObject<List<Enemy>>(obj.Data["Enemies"]);
 foreach (var enemy in Enemies)
 {
 Debug.Log(enemy.name);
 }
}

obj.Data is a Dictionary, its key is "Enemies" and the value is your enemy array. So you will need to deserialize it as List<Enemy>. JsonUtility does not support array, so here you can use the JsonWrapper provided by PlayFab.


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.

el-inside avatar image el-inside commented ·

awesome, it works

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.