Retrieve data from firebase database to nested dictionary












0














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?










share|improve this question
























  • Your object does not match your json .Use this to generate the proper objects to de-serialize your class with then read the duplicate for how to de-serialize it. If you run into problem, read the troubleshooting section from the duplicate. Finally, you can edit this question with your new code and let me know if it's now working for you.
    – Programmer
    Nov 15 '18 at 17:37










  • Hi, Thanks for the answer. It isn't exactly working, as effectively json2csharp give me the objects that this concrete json needs. But what is I have an undefined number of "group" key and undefined number of "id_" key? json2csharp gives to me something like this: public class Id1 {public string Question { get; set; } public string A { get; set; } } public class Id2 {public string Question { get; set; } public string A { get; set; }} public class Group1 {public Id1 id_1 { get; set; } public Id2 id_2 { get; set; }} public class RootObject { public Group1 group1 { get; set; } }
    – ktmlleska
    Nov 15 '18 at 17:50










  • Edit your question then add "EDIT" followed by your new code. Don't post code in the comment section. Shoe your new object and how you're de-serializing them. Finally, make sure to read the troubleshooting section from the duplicate. You didn't. If you did, you wouldn't have { get; set; }. Also, if you've read it [Serializable] would be on top of each class you generated.
    – Programmer
    Nov 15 '18 at 17:58










  • Sorry, but I can't find the troubleshooting section... is it in this page? json2csharp.com/# or you talking about this one? app.quicktype.io/#l=cs&r=json2csharp
    – ktmlleska
    Nov 15 '18 at 18:04










  • I marked this question as a duplicate. Refresh the page and look at your question. It will tell you that the question has been closed and will point to this question. That's what you should be looking at
    – Programmer
    Nov 15 '18 at 18:16
















0














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?










share|improve this question
























  • Your object does not match your json .Use this to generate the proper objects to de-serialize your class with then read the duplicate for how to de-serialize it. If you run into problem, read the troubleshooting section from the duplicate. Finally, you can edit this question with your new code and let me know if it's now working for you.
    – Programmer
    Nov 15 '18 at 17:37










  • Hi, Thanks for the answer. It isn't exactly working, as effectively json2csharp give me the objects that this concrete json needs. But what is I have an undefined number of "group" key and undefined number of "id_" key? json2csharp gives to me something like this: public class Id1 {public string Question { get; set; } public string A { get; set; } } public class Id2 {public string Question { get; set; } public string A { get; set; }} public class Group1 {public Id1 id_1 { get; set; } public Id2 id_2 { get; set; }} public class RootObject { public Group1 group1 { get; set; } }
    – ktmlleska
    Nov 15 '18 at 17:50










  • Edit your question then add "EDIT" followed by your new code. Don't post code in the comment section. Shoe your new object and how you're de-serializing them. Finally, make sure to read the troubleshooting section from the duplicate. You didn't. If you did, you wouldn't have { get; set; }. Also, if you've read it [Serializable] would be on top of each class you generated.
    – Programmer
    Nov 15 '18 at 17:58










  • Sorry, but I can't find the troubleshooting section... is it in this page? json2csharp.com/# or you talking about this one? app.quicktype.io/#l=cs&r=json2csharp
    – ktmlleska
    Nov 15 '18 at 18:04










  • I marked this question as a duplicate. Refresh the page and look at your question. It will tell you that the question has been closed and will point to this question. That's what you should be looking at
    – Programmer
    Nov 15 '18 at 18:16














0












0








0







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?










share|improve this question















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?







json firebase dictionary unity3d firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 9:50







ktmlleska

















asked Nov 15 '18 at 13:25









ktmlleskaktmlleska

13




13












  • Your object does not match your json .Use this to generate the proper objects to de-serialize your class with then read the duplicate for how to de-serialize it. If you run into problem, read the troubleshooting section from the duplicate. Finally, you can edit this question with your new code and let me know if it's now working for you.
    – Programmer
    Nov 15 '18 at 17:37










  • Hi, Thanks for the answer. It isn't exactly working, as effectively json2csharp give me the objects that this concrete json needs. But what is I have an undefined number of "group" key and undefined number of "id_" key? json2csharp gives to me something like this: public class Id1 {public string Question { get; set; } public string A { get; set; } } public class Id2 {public string Question { get; set; } public string A { get; set; }} public class Group1 {public Id1 id_1 { get; set; } public Id2 id_2 { get; set; }} public class RootObject { public Group1 group1 { get; set; } }
    – ktmlleska
    Nov 15 '18 at 17:50










  • Edit your question then add "EDIT" followed by your new code. Don't post code in the comment section. Shoe your new object and how you're de-serializing them. Finally, make sure to read the troubleshooting section from the duplicate. You didn't. If you did, you wouldn't have { get; set; }. Also, if you've read it [Serializable] would be on top of each class you generated.
    – Programmer
    Nov 15 '18 at 17:58










  • Sorry, but I can't find the troubleshooting section... is it in this page? json2csharp.com/# or you talking about this one? app.quicktype.io/#l=cs&r=json2csharp
    – ktmlleska
    Nov 15 '18 at 18:04










  • I marked this question as a duplicate. Refresh the page and look at your question. It will tell you that the question has been closed and will point to this question. That's what you should be looking at
    – Programmer
    Nov 15 '18 at 18:16


















  • Your object does not match your json .Use this to generate the proper objects to de-serialize your class with then read the duplicate for how to de-serialize it. If you run into problem, read the troubleshooting section from the duplicate. Finally, you can edit this question with your new code and let me know if it's now working for you.
    – Programmer
    Nov 15 '18 at 17:37










  • Hi, Thanks for the answer. It isn't exactly working, as effectively json2csharp give me the objects that this concrete json needs. But what is I have an undefined number of "group" key and undefined number of "id_" key? json2csharp gives to me something like this: public class Id1 {public string Question { get; set; } public string A { get; set; } } public class Id2 {public string Question { get; set; } public string A { get; set; }} public class Group1 {public Id1 id_1 { get; set; } public Id2 id_2 { get; set; }} public class RootObject { public Group1 group1 { get; set; } }
    – ktmlleska
    Nov 15 '18 at 17:50










  • Edit your question then add "EDIT" followed by your new code. Don't post code in the comment section. Shoe your new object and how you're de-serializing them. Finally, make sure to read the troubleshooting section from the duplicate. You didn't. If you did, you wouldn't have { get; set; }. Also, if you've read it [Serializable] would be on top of each class you generated.
    – Programmer
    Nov 15 '18 at 17:58










  • Sorry, but I can't find the troubleshooting section... is it in this page? json2csharp.com/# or you talking about this one? app.quicktype.io/#l=cs&r=json2csharp
    – ktmlleska
    Nov 15 '18 at 18:04










  • I marked this question as a duplicate. Refresh the page and look at your question. It will tell you that the question has been closed and will point to this question. That's what you should be looking at
    – Programmer
    Nov 15 '18 at 18:16
















Your object does not match your json .Use this to generate the proper objects to de-serialize your class with then read the duplicate for how to de-serialize it. If you run into problem, read the troubleshooting section from the duplicate. Finally, you can edit this question with your new code and let me know if it's now working for you.
– Programmer
Nov 15 '18 at 17:37




Your object does not match your json .Use this to generate the proper objects to de-serialize your class with then read the duplicate for how to de-serialize it. If you run into problem, read the troubleshooting section from the duplicate. Finally, you can edit this question with your new code and let me know if it's now working for you.
– Programmer
Nov 15 '18 at 17:37












Hi, Thanks for the answer. It isn't exactly working, as effectively json2csharp give me the objects that this concrete json needs. But what is I have an undefined number of "group" key and undefined number of "id_" key? json2csharp gives to me something like this: public class Id1 {public string Question { get; set; } public string A { get; set; } } public class Id2 {public string Question { get; set; } public string A { get; set; }} public class Group1 {public Id1 id_1 { get; set; } public Id2 id_2 { get; set; }} public class RootObject { public Group1 group1 { get; set; } }
– ktmlleska
Nov 15 '18 at 17:50




Hi, Thanks for the answer. It isn't exactly working, as effectively json2csharp give me the objects that this concrete json needs. But what is I have an undefined number of "group" key and undefined number of "id_" key? json2csharp gives to me something like this: public class Id1 {public string Question { get; set; } public string A { get; set; } } public class Id2 {public string Question { get; set; } public string A { get; set; }} public class Group1 {public Id1 id_1 { get; set; } public Id2 id_2 { get; set; }} public class RootObject { public Group1 group1 { get; set; } }
– ktmlleska
Nov 15 '18 at 17:50












Edit your question then add "EDIT" followed by your new code. Don't post code in the comment section. Shoe your new object and how you're de-serializing them. Finally, make sure to read the troubleshooting section from the duplicate. You didn't. If you did, you wouldn't have { get; set; }. Also, if you've read it [Serializable] would be on top of each class you generated.
– Programmer
Nov 15 '18 at 17:58




Edit your question then add "EDIT" followed by your new code. Don't post code in the comment section. Shoe your new object and how you're de-serializing them. Finally, make sure to read the troubleshooting section from the duplicate. You didn't. If you did, you wouldn't have { get; set; }. Also, if you've read it [Serializable] would be on top of each class you generated.
– Programmer
Nov 15 '18 at 17:58












Sorry, but I can't find the troubleshooting section... is it in this page? json2csharp.com/# or you talking about this one? app.quicktype.io/#l=cs&r=json2csharp
– ktmlleska
Nov 15 '18 at 18:04




Sorry, but I can't find the troubleshooting section... is it in this page? json2csharp.com/# or you talking about this one? app.quicktype.io/#l=cs&r=json2csharp
– ktmlleska
Nov 15 '18 at 18:04












I marked this question as a duplicate. Refresh the page and look at your question. It will tell you that the question has been closed and will point to this question. That's what you should be looking at
– Programmer
Nov 15 '18 at 18:16




I marked this question as a duplicate. Refresh the page and look at your question. It will tell you that the question has been closed and will point to this question. That's what you should be looking at
– Programmer
Nov 15 '18 at 18:16












1 Answer
1






active

oldest

votes


















0














If someone has the same problem.
What I found to do what I was looking for, is that JsonUtility from unity can't do it because it just work with a knowing keys as you have always to create your objects as a classes in unity.
So, if for example my json has the "id_3" key it will not work because my objects only have "id_1" and "id_2". The only way to be able to create an undefined numbers of Ids is making them an arrays (which I don't want in this case) because it will make you call the dictionary something like this: myDictionary[0]["Question"]
But anyway, just if someone needs it, the Json should be like this to work with JsonUtility (setting the Ids as an array):



{"questions": {
"group1": [
{
"Question": "question one",
"A": "answer1"
},
{
"Question": "question two",
"A": "answer2"
}
]
}
}


Well, as this isn't what I was looking for, the easiest way I found to do it is using the SimpleJson library which does exactly what I expected and it is super easy to use, without needed to create your own objects in unity.
With a normal Json file coming from the snapshot of the firebase:



{"questions": {
"group1": {
"id_1": {
"Question": "question one",
"A": "answer1"
},
"id_2": {
"Question": "question two",
"A": "answer2"
}
}
}
}


You just have to do the following:



var N = JSON.Parse(snapshot.GetRawJsonValue());
Debug.Log("N---> " + N["id_1"].ToString());


So it does exactly what I expected.
The bad thing here is that this is an external library, so it could have some problems in unity for example trying to use syncVars with this data.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320515%2fretrieve-data-from-firebase-database-to-nested-dictionary%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If someone has the same problem.
    What I found to do what I was looking for, is that JsonUtility from unity can't do it because it just work with a knowing keys as you have always to create your objects as a classes in unity.
    So, if for example my json has the "id_3" key it will not work because my objects only have "id_1" and "id_2". The only way to be able to create an undefined numbers of Ids is making them an arrays (which I don't want in this case) because it will make you call the dictionary something like this: myDictionary[0]["Question"]
    But anyway, just if someone needs it, the Json should be like this to work with JsonUtility (setting the Ids as an array):



    {"questions": {
    "group1": [
    {
    "Question": "question one",
    "A": "answer1"
    },
    {
    "Question": "question two",
    "A": "answer2"
    }
    ]
    }
    }


    Well, as this isn't what I was looking for, the easiest way I found to do it is using the SimpleJson library which does exactly what I expected and it is super easy to use, without needed to create your own objects in unity.
    With a normal Json file coming from the snapshot of the firebase:



    {"questions": {
    "group1": {
    "id_1": {
    "Question": "question one",
    "A": "answer1"
    },
    "id_2": {
    "Question": "question two",
    "A": "answer2"
    }
    }
    }
    }


    You just have to do the following:



    var N = JSON.Parse(snapshot.GetRawJsonValue());
    Debug.Log("N---> " + N["id_1"].ToString());


    So it does exactly what I expected.
    The bad thing here is that this is an external library, so it could have some problems in unity for example trying to use syncVars with this data.






    share|improve this answer




























      0














      If someone has the same problem.
      What I found to do what I was looking for, is that JsonUtility from unity can't do it because it just work with a knowing keys as you have always to create your objects as a classes in unity.
      So, if for example my json has the "id_3" key it will not work because my objects only have "id_1" and "id_2". The only way to be able to create an undefined numbers of Ids is making them an arrays (which I don't want in this case) because it will make you call the dictionary something like this: myDictionary[0]["Question"]
      But anyway, just if someone needs it, the Json should be like this to work with JsonUtility (setting the Ids as an array):



      {"questions": {
      "group1": [
      {
      "Question": "question one",
      "A": "answer1"
      },
      {
      "Question": "question two",
      "A": "answer2"
      }
      ]
      }
      }


      Well, as this isn't what I was looking for, the easiest way I found to do it is using the SimpleJson library which does exactly what I expected and it is super easy to use, without needed to create your own objects in unity.
      With a normal Json file coming from the snapshot of the firebase:



      {"questions": {
      "group1": {
      "id_1": {
      "Question": "question one",
      "A": "answer1"
      },
      "id_2": {
      "Question": "question two",
      "A": "answer2"
      }
      }
      }
      }


      You just have to do the following:



      var N = JSON.Parse(snapshot.GetRawJsonValue());
      Debug.Log("N---> " + N["id_1"].ToString());


      So it does exactly what I expected.
      The bad thing here is that this is an external library, so it could have some problems in unity for example trying to use syncVars with this data.






      share|improve this answer


























        0












        0








        0






        If someone has the same problem.
        What I found to do what I was looking for, is that JsonUtility from unity can't do it because it just work with a knowing keys as you have always to create your objects as a classes in unity.
        So, if for example my json has the "id_3" key it will not work because my objects only have "id_1" and "id_2". The only way to be able to create an undefined numbers of Ids is making them an arrays (which I don't want in this case) because it will make you call the dictionary something like this: myDictionary[0]["Question"]
        But anyway, just if someone needs it, the Json should be like this to work with JsonUtility (setting the Ids as an array):



        {"questions": {
        "group1": [
        {
        "Question": "question one",
        "A": "answer1"
        },
        {
        "Question": "question two",
        "A": "answer2"
        }
        ]
        }
        }


        Well, as this isn't what I was looking for, the easiest way I found to do it is using the SimpleJson library which does exactly what I expected and it is super easy to use, without needed to create your own objects in unity.
        With a normal Json file coming from the snapshot of the firebase:



        {"questions": {
        "group1": {
        "id_1": {
        "Question": "question one",
        "A": "answer1"
        },
        "id_2": {
        "Question": "question two",
        "A": "answer2"
        }
        }
        }
        }


        You just have to do the following:



        var N = JSON.Parse(snapshot.GetRawJsonValue());
        Debug.Log("N---> " + N["id_1"].ToString());


        So it does exactly what I expected.
        The bad thing here is that this is an external library, so it could have some problems in unity for example trying to use syncVars with this data.






        share|improve this answer














        If someone has the same problem.
        What I found to do what I was looking for, is that JsonUtility from unity can't do it because it just work with a knowing keys as you have always to create your objects as a classes in unity.
        So, if for example my json has the "id_3" key it will not work because my objects only have "id_1" and "id_2". The only way to be able to create an undefined numbers of Ids is making them an arrays (which I don't want in this case) because it will make you call the dictionary something like this: myDictionary[0]["Question"]
        But anyway, just if someone needs it, the Json should be like this to work with JsonUtility (setting the Ids as an array):



        {"questions": {
        "group1": [
        {
        "Question": "question one",
        "A": "answer1"
        },
        {
        "Question": "question two",
        "A": "answer2"
        }
        ]
        }
        }


        Well, as this isn't what I was looking for, the easiest way I found to do it is using the SimpleJson library which does exactly what I expected and it is super easy to use, without needed to create your own objects in unity.
        With a normal Json file coming from the snapshot of the firebase:



        {"questions": {
        "group1": {
        "id_1": {
        "Question": "question one",
        "A": "answer1"
        },
        "id_2": {
        "Question": "question two",
        "A": "answer2"
        }
        }
        }
        }


        You just have to do the following:



        var N = JSON.Parse(snapshot.GetRawJsonValue());
        Debug.Log("N---> " + N["id_1"].ToString());


        So it does exactly what I expected.
        The bad thing here is that this is an external library, so it could have some problems in unity for example trying to use syncVars with this data.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 9:38

























        answered Nov 16 '18 at 9:32









        ktmlleskaktmlleska

        13




        13






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320515%2fretrieve-data-from-firebase-database-to-nested-dictionary%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)