philipsniche
philipsniche

Reputation: 23

How to access nested JSON with array in firebase

I want to access a JSON of this structure in firebase

The structure

    {
    "questions":{
         "English":{
              "English_2002":[
                {
                 "correct_ans":"A",
                 "OptionA":"a coder",
                  "OptionB":"a hacker",
                   "OptionC":"a writer",
                   "OptionD":"a programmer",
                    "Question":"Who build         software"
                },
                {},
                {}
              ],
               "English_2003":[],


            }
      }
}

I want this structure. In the subject structure, other subjects will come after I exhaust 9 years of English.

My confusion is how to logically get each subject since firebase will only accept the root name questions.

Please I may sound dumb, but I have a very long questions thread almost 55000 lines. Because firebase accept one JSON tree.

Sorry i wasn't very clear i was asking from the stack phone app:

I have a question json tag of the structure above; my question is how will i be able to access the object subject like "english":{ // then accessing the first english array "english":[] //since am now using firebase.

}

initially each array was individual json file, i have to recreate them into one for firebase sake. this is how i was parsing it then.

public class QuestionParser {
Context context;


public QuestionParser(Context c) {
    this.context = c;
}



public ArrayList<Question> getJsonFromUrl(String url, String arrayName)
{
    ArrayList<Question> arrayofQuestion = new ArrayList<>();
    return arrayofQuestion;
}

// Processing question from JSon file in res > raw folder
public ArrayList<Question> parseQuestionJson(int rawJsonFileId, String arrayName) {
    ArrayList<Question> questionList = new ArrayList<>();
    String jsonstr = null;
    try {
        InputStream in = context.getResources().openRawResource(rawJsonFileId);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        jsonstr = sb.toString();
        Log.d("REEEEADDD" + this.toString(), jsonstr);
        //System.out.println(jsonstr);

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // If the JSON string is empty or null, then return early.

    if (TextUtils.isEmpty(jsonstr)) {
        return null;
    }
    try {

        JSONObject jsonObject = new JSONObject(jsonstr);


        JSONArray jsonArray = jsonObject.getJSONArray(arrayName);

        JSONObject jobject;
        for (int i = 0; i < jsonArray.length(); i++) {
            // TEST


            jobject = jsonArray.getJSONObject(i);
            String ans = jobject.getString("correct_answer");
            String graphic_name = jobject.getString("question_image");
            String optionA = jobject.getString("optiona");
            String optionB = jobject.getString("optionb");
            String optionC = jobject.getString("optionc");
            String optionD = jobject.getString("optiond");
            String questionNo = jobject.getString("question_number");
            String question = jobject.getString("question");

            questionList.add(new Question(questionNo, graphic_name, question, optionA, optionB, optionC, optionD, ans));
            Log.d("DDD" + this.toString(), String.valueOf(questionList.get(i)));


        }
        Log.i("ONE QUESTION", questionList.get(50).getQuestion());
    } catch (Exception e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return questionList;
}

}

So how can i parse it from firebase because initially, if a student chooses question and year i passes those value as parameter and use them for parsing. but in firebase now i have access to only root firebase name in the get reference e method

Upvotes: 0

Views: 718

Answers (1)

PentaKon
PentaKon

Reputation: 4636

To access for example "correct_ans":"A" you would query your firebase like so:

your.firebase.domain/questions/English/English_2002/0/correct_ans

Notice that each level in the json object is represented by a / and the key you want to access whereas in case of an array you simple add the array index. JSON's simple structure also allows simple REST like access

Upvotes: 1

Related Questions