我想在firebase中访问此结构的JSON
结构
{
"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":[],
}
}
}
我想要这个结构。在学科结构中,其他科目将在我用完9年的英语后出现。
我的困惑是如何逻辑地获取每个主题,因为firebase只接受根名称问题。
我可能听起来很愚蠢,但我有一个很长的问题线程近55000行。因为firebase接受一个JSON树。
抱歉,我不是很清楚我是从堆叠手机应用程序询问的:
我有一个上面结构的问题json标签;我的问题是我将如何访问像“english”这样的对象主题:{ //然后访问第一个英文数组“english”:[] //因为我现在正在使用firebase。
}
最初每个数组都是单独的json文件,为了firebase,我必须将它们重新创建为一个。这就是我解析它的方式。
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;
}
}
那么我如何从firebase解析它,因为最初,如果学生选择问题,那么我将这些值作为参数传递并使用它们进行解析。但是在firebase中,我现在只能访问get reference e方法中的root firebase名称
答案 0 :(得分:1)
要访问例如&#34; correct_ans&#34;:&#34; A&#34;你会像这样查询你的firebase:
your.firebase.domain/questions/English/English_2002/0/correct_ans
请注意,json对象中的每个级别都由/
和您要访问的键表示,而在数组的情况下,您可以简单地添加数组索引。 JSON的简单结构还允许简单的REST
类访问