I'm having a lots of problems trying to retrieve data from the firebase database and moving that data to a unity dictionary.
So I would like to do this, to just retrieve the data one time and then place all the data to a dictionary, the problem is that I can't find the way to do it if the dictionary is a nested dictionary.
This is how my database looks like (simplified):
{"questions": {
"group1": {
"id_1": {
"Question": "question one",
"A": "answer1"
},
"id_2": {
"Question": "question two",
"A": "answer2"
}
}
}
}
So in my code I do the following:
public void RetrievePreguntesJson()
{
string group = "group1"
questions_ref.Child(group).GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
Debug.LogError("Error Getting Info from user database");
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
myDictionary = snapshot.Value as Dictionary<string, object>;
dataReady();
}
});
}
So the problem is that I can't get any value.
I would like to do something like myDictionary["id_1"]["Question"]
and get the value "question one". Or myDictionary["id_1"].Question
and get the value "question one".
I already tried the following: - Create a Class questions like this:
public class Questions
{
public string id;
public string Question;
public string Answer;
public Questions(string id, string Question, string Answer)
{
this.id = id;
this.Question = Question;
this.Answer = Answer;
}
}
And use the following code instead of "userInfoDictionary = snapshot.Value as Dictionary<string, object>;"
Use:
Questions ClassObjectName = JsonUtility.FromJson<Questions>(snapshot.GetRawJsonValue());
But it didn't work either.
Also tried:
"userInfoDictionary = snapshot.Value as Dictionary<string, Dictionary<string, object>>;"
But it won't work either.
Can anyone help me please?
"EDIT"
This is my json:
{"questions": {
"group1": {
"id_1": {
"Question": "question one",
"A": "answer1"
},
"id_2": {
"Question": "question two",
"A": "answer2"
}
}
}
}
And this are the objects:
[System.Serializable]
public class Group1
{
public QuestionsId group1;
}
[System.Serializable]
public class QuestionsId
{
public Questions id;
}
[System.Serializable]
public class Questions
{
public string Question;
public string A;
}
I'm deserializing the Json with:
Group1 ClassObjectName = JsonUtility.FromJson<Group1>(snapshot.GetRawJsonValue());
I would like to be able to have a result calling the values like this:
myDictionary["id_1"].Question
myDictionary["id_1"]["Question"]
I'm using the object and the Json utility now, but if there is anyway to use a unity dictionary to have the result that I would like to have it is also ok for me. For example: "userInfoDictionary = snapshot.Value as Dictionary<string, object>;"
Does anyone knows how to help me please?