Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Problem getting firebase dictionaries

Discussion in 'Scripting' started by TheDraker, Aug 14, 2019 at 1:22 AM.

  1. TheDraker

    TheDraker

    Joined:
    May 16, 2019
    Posts:
    1
    Hello!

    I'm having trouble getting a list of firebase dictionaries. Objective: List character skins that are stored in the "birds" attribute. Other attributes like "username" I get correctly. But I'm getting null from the birds attribute.

    Follow my code:


    Code (CSharp):
    1. reference.Child("users")
    2.             .Child(userId)
    3.             .GetValueAsync()
    4.             .ContinueWith(task =>
    5.             {
    6.  
    7.             if (task.IsFaulted)
    8.             {
    9.                 Debug.LogError("CHECKUSER: TASK IS FAULTED");
    10.             }
    11.  
    12.             if (task.IsCompleted)
    13.             {
    14.  
    15.                 /******* Result ********/
    16.                 DataSnapshot snapshot = task.Result;
    17.                    
    18.  
    19.                 /******* Not find ********/
    20.                 if (snapshot.GetValue(true) == null)
    21.                 {
    22.                     Debug.LogWarning("GetUser: the user do not exists.");
    23.                     //callbackFunction(null);
    24.  
    25.                 }
    26.                 /******* If find ********/
    27.                 else
    28.                 {
    29.                         Debug.LogWarning("GetUser: the user has found.");
    30.  
    31.                     /******* JSON PARSE in USER class ********/
    32.                    
    33.                     string snapshotJson = snapshot.GetRawJsonValue();
    34.                     User deserializedUser = new User();
    35.  
    36.                     MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(snapshotJson));
    37.  
    38.                     DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());
    39.                    
    40.                     deserializedUser = ser.ReadObject(ms) as User;
    41.                     ms.Close();
    42.  
    43.                     user = deserializedUser;
    44.  
    45.                    
    46.                     Debug.Log(user.username);
    47.                     Debug.Log(user.birds);
    48.                     Debug.Log(user.birds.Count);
    49.  
    50. /* result :
    51.  
    52. TheDraker7
    53. UnityEngine.Debug:Log(Object)
    54.  
    55. System.Collections.Generic.Dictionary`2[System.String,System.Object]
    56. UnityEngine.Debug:Log(Object)
    57.  
    58. 0
    59. UnityEngine.Debug:Log(Object)
    60.  
    61. */
    62.  
    63.                     }
    64.                 }
    65.             });

    Follow the User class:

    Code (CSharp):
    1. public class User
    2. {
    3.     public string userId { get; set; }
    4.     public string username { get; set; }
    5.     public string highscore { get; set; }
    6.     public int coins { get; set; }
    7.  
    8.     public IDictionary<string, object> birds;
    9.     public List<string> weapons = new List<string>();
    10.  
    11.     public User()
    12.     {
    13.  
    14.     }
    15.  
    16.     public User(string username, string highscore, int coins, IDictionary<string, object> birds, List<string> weapons)
    17.     {
    18.         this.username = username;
    19.         this.highscore = highscore;
    20.         this.coins = coins;
    21.         this.birds = birds;
    22.         // this.weapons = weapons;
    23.     }
    Follow the Firebase data I want to get:

    {
    "users":{
    "teste":{
    "birds":{
    "0":"default",
    "kongetoucan":"kongetoucan"
    },
    "coins":301,
    "highscore":"3",
    "userId":"teste",
    "username":"TheDraker7"
    }
    }
    }



    Please, can you help me?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    What class do you expect the deserializer to instatiate in case when target field type is interface? I think you should try with Dictionary instead.