2017-06-01 96 views
0

想在火力我如何使用數組訪問嵌套的JSON在火力

訪問此結構的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法

+0

我不知道你在這裏試圖做什麼,也沒有什麼問題與它。你可以顯示[最小的代碼,顯示你卡住的地方](http://stackoverflow.com/help/mcve)?這通常是獲得Stack Overflow編碼問題幫助的最快方法。 –

+0

@FrankvanPuffelen謝謝,我已經更新了這個問題,我最初是從堆棧移動應用程序 – philipsniche

回答

1

要訪問例如「correct_ans」:「A」,你會質疑你的火力點,像這樣:

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

請注意,json對象中的每個級別都由/和您想要訪問的鍵表示,而在數組的情況下,您可以簡單地添加數組索引。 JSON的簡單結構還允許簡單的REST像訪問

+0

問的謝謝@Konstantine。我的意思是說,我有2個微調1用於一年的學科。所以我的問題我得到了來自用戶的2值,我如何在樹中得到這樣的問題。我用圖像更新了這個問題。檢查它,並幫助我請。謝謝。 – philipsniche

+0

圖片失敗,我的聲望很低。對不起, – philipsniche

+0

你的意思是用戶給你2個參數嗎?例如'subject =「English」'和'year = 2002'?如果是,那麼你可以像這樣形成一個url:''your.firebase.domain/qustions /「+ subject +」/「+ subject +」_「+ year +」/ 0/correct_ans「' – Konstantine