我如何使用数组访问嵌套的JSON在火力

我如何使用数组访问嵌套的JSON在火力

问题描述:

想在火力我如何使用数组访问嵌套的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://*.com/help/mcve)?这通常是获得Stack Overflow编码问题帮助的最快方法。 –

+0

@FrankvanPuffelen谢谢,我已经更新了这个问题,我最初是从堆栈移动应用程序 – philipsniche

要访问例如“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