訪問此結構的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標籤;我的問題是我將如何訪問像「英語」對象的主題:{ //然後訪問第一個英文數組「英語」:[] //因爲我現在使用的是firebase。
}
最初每個陣列是單個JSON文件,我必須將它們重新創建成一個用於火力緣故。這就是我當時解析它的方式。
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;
}
}
因此,如何我可以分析它從火力點,因爲一開始,如果學生選擇的問題,今年,我通過這些值作爲參數,並將其用於分析。但在火力現在我只能訪問根火力名在獲取參考E法
我不知道你在這裏試圖做什麼,也沒有什麼問題與它。你可以顯示[最小的代碼,顯示你卡住的地方](http://stackoverflow.com/help/mcve)?這通常是獲得Stack Overflow編碼問題幫助的最快方法。 –
@FrankvanPuffelen謝謝,我已經更新了這個問題,我最初是從堆棧移動應用程序 – philipsniche