从json获取对象/数组之后,我该如何改变它(Randomize)

时间:2015-10-03 05:44:45

标签: android json random

我使用此

获取了json数据

``

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");
        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;
    }

``

这是json数据。

``

{
  "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"
        }
      ]
    },
    ]
}

获取json数据后 我现在需要的是将它随机化。 请帮助一位兄弟,我已经尝试了一切,但没有任何工作 提前致谢

2 个答案:

答案 0 :(得分:1)

quesList = quesObj.getJSONArray("Questions"); // Right place to shuffle PeterOla,将其添加到随机问题列表中:

    List<JSONObject> questionsList = new ArrayList<JSONObject>(quesList.length());
    for(int i=0,size=quesList.length();i<size;++i){
        try {
            questionsList.add(quesList.getJSONObject(i));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    long seed = System.nanoTime();
    Collections.shuffle(questionsList, new Random(seed));

答案 1 :(得分:1)

将值放入列表中,然后您可以轻松地将其洗牌。使用此:

$mysqli = new mysqli('localhost', 'root', '', 'demo');  
$stmt = $mysqli -> prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt = $stmt->get_result();
while ($row = $stmt->fetch_assoc()) {
    //do something with row
}

$stmt->bind_param('i', $id2);
$stmt->execute();
$stmt = $stmt->get_result();
while ($row = $stmt->fetch_assoc()) {
    //do something with row
}