I'm trying to randomize JSON array this way:
ArrayList<Integer> intArr1 = new ArrayList<>(myJSONarr.length());
for (int i = 0; i < myJSONarr.length(); i++) {
intArr1.add(i);
}
Collections.shuffle(intArr1);
for (int i = 0; i < intArr1.size(); i++) {
myJSONarr.put(i, myJSONarr.getJSONObject(intArr1.get(i)));
if (i == (myJSONarr.length() - 1))
break;
}
As a result of this not only JSON objects are randomizing, but also keys and values. I mean, I had an array like this:
[ {"id":"0","hieroglyph":"水","pinyin":"Shuǐ","pinyin_num":"Shui3","russian":["вода"],"hsk":"1"}, {"id":"1","hieroglyph":"人","pinyin":"Rén","pinyin_num":"Re2n","russian":["человек"],"hsk":"1"}, {"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}, {"id":"3","hieroglyph":"不","pinyin":"Bù","pinyin_num":"Bu4","russian":["нет"],"hsk":"1"}, {"id":"4","hieroglyph":"少","pinyin":"Shǎo","pinyin_num":"Sha3o","russian":["мало"],"hsk":"1"}]
And after randomize it looks like this
[{"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}, {"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}, {"id":"4","hieroglyph":"少","pinyin":"Shǎo","pinyin_num":"Sha3o","russian":["мало"],"hsk":"1"}, {"id":"3","hieroglyph":"不","pinyin":"Bù","pinyin_num":"Bu4","russian":["нет"],"hsk":"1"}, {"id":"2","hieroglyph":"日","pinyin":"Rì","pinyin_num":"Ri4","russian":["день"],"hsk":"1"}]
Aucun commentaire:
Enregistrer un commentaire