vendredi 2 octobre 2015

After Getting object/array from json, how can i shuffle it (Randomize)

I got the json data using this

``private void loadQuestions() throws Exception { try { InputStream questions = this.getBaseContext().getResources() .openRawResource(R.raw.questions); bReader = new BufferedReader(new InputStreamReader(questions)); StringBuilder quesString = new StringBuilder(); String aJsonLine = null; while ((aJsonLine = bReader.readLine()) != null) { quesString.append(aJsonLine); } Log.d(this.getClass().toString(), quesString.toString()); JSONObject quesObj = new JSONObject(quesString.toString()); quesList = quesObj.getJSONArray("Questions"); // Right place to shuffle PeterOla

    Log.d(this.getClass().getName(),
            "Num Questions " + quesList.length());
    } catch (Exception e){

    } finally {
        try {
            bReader.close();
        } catch (Exception e) {
            Log.e("", e.getMessage().toString(), e.getCause());
        }

    }


}

public static JSONArray getQuesList() {

    return quesList;
}``

Here is the json data.

``{ "Questions": [ { "Question": "Which animal is Carnivorous?", "CorrectAnswer": 1, "Answers": [ { "Answer": "Cow" }, { "Answer": "Lion" }, { "Answer": "Goat" }, { "Answer": "Elephant" } ] }, { "Question": "Humans need", "CorrectAnswer": 0, "Answers": [ { "Answer": "Oxygen" }, { "Answer": "Nitrogen" }, { "Answer": "CarbonDioxide" }, { "Answer": "Hydrogen" } ] },

    {
  "Question": "Choose the Amphibian ",
  "CorrectAnswer": 0,
  "Answers": [
    {
      "Answer": "Frog"
    },
    {
      "Answer": "Owl"
    },
    {
      "Answer": "Goat"
    },
    {
      "Answer": "Fox"
    }
  ]
},

    {
  "Question": "---- is part of Earth`s Atmosphere.",
  "CorrectAnswer": 1,
  "Answers": [
    {
      "Answer": "Unisphere"
    },
    {
      "Answer": "Troposphere"
    },
    {
      "Answer": "Oxysphere"
    },
    {
      "Answer": "Carbosphere"
    }
  ]
},
]

}``

All I need now is to randomize it. Help a brother please, I have everything but nothing is working




Aucun commentaire:

Enregistrer un commentaire