0

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

2
  • I have no idea what you're trying to do here, nor what problem you have with it. Can you show the minimal code that shows where you are stuck? That is generally the fastest way to get help with coding problems on Stack Overflow. Commented Jun 1, 2017 at 22:49
  • @FrankvanPuffelen Thanks i have updated the question, I was initially asking from stack mobile app Commented Jun 2, 2017 at 23:15

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you @Konstantine. What I mean is I have 2 spinner 1for subject one for year. So my issue I get the 2 value from a user how do I get to such question in the tree. I updated the question with image. Check it and help me pls. thanks.
The image failed cos my reputation is low. Sorry about that
Do you mean the user gives you 2 parameters? For example subject = "English" and year = 2002 ? If yes then you can form a url like so: "your.firebase.domain/qustions/" + subject + "/" + subject + "_" + year + "/0/correct_ans"
exactly. i will take parameters to process the specific year. then add it to arraylist and he can now start a quiz on the question
Yes! If I get to that path I will get the whole question of the year of choice and add it to list. For each choice by student by year and subject I will repeat the procedure. So how can I make d url so I could be passing the choice as in the URL very close to what you were showing me up. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.