How to get multiple JSONObject with the same key?

2

Basically, I have this JSON:

{"gameId":467366059,"mapId":11,"gameMode":"CLASSIC","gameType":"MATCHED_GAME","gameQueueConfigId":4,"participants":[{"teamId":100,"spell1Id":4,"spell2Id":14,"championId":157,"profileIconId":588,"summonerName":"MEDAUMASKINCOPY","bot":false,"**summonerId**":2833743,"runes":[{"count":6,"runeId":5247},{"count":3,"runeId":5251},{"count":9,"runeId":5289},{"count":9,"runeId":5317},{"count":1,"runeId":5337},{"count":2,"runeId":5412}],"masteries":[{"rank":1,"masteryId":4111},{"rank":4,"masteryId":4112},{"rank":3,"masteryId":4122},{"rank":1,"masteryId":4131},{"rank":1,"masteryId":4132},{"rank":3,"masteryId":4134},{"rank":1,"masteryId":4141},{"rank":3,"masteryId":4142},{"rank":1,"masteryId":4144},{"rank":2,"masteryId":4152},{"rank":1,"masteryId":4162},{"rank":2,"masteryId":4211},{"rank":2,"masteryId":4212},{"rank":1,"masteryId":4221},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4232}]},{"teamId":100,"spell1Id":11,"spell2Id":4,"championId":254,"profileIconId":550,"summonerName":"copy","bot":false,"**summonerId**":2883696,"runes":[{"count":9,"runeId":5247},{"count":3,"runeId":5290},{"count":6,"runeId":5295},{"count":9,"runeId":5315},{"count":3,"runeId":5335}],"masteries":[{"rank":1,"masteryId":4111},{"rank":4,"masteryId":4113},{"rank":3,"masteryId":4122},{"rank":1,"masteryId":4131},{"rank":1,"masteryId":4132},{"rank":3,"masteryId":4134},{"rank":1,"masteryId":4141},{"rank":3,"masteryId":4142},{"rank":3,"masteryId":4152},{"rank":1,"masteryId":4162},{"rank":2,"masteryId":4213},{"rank":2,"masteryId":4214},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4224},{"rank":1,"masteryId":4232}]},{"teamId":100,"spell1Id":4,"spell2Id":12,"championId":85,"profileIconId":23,"summonerName":"Yzy Zikaa","bot":false,"**summonerId**":1356728,"runes":[{"count":1,"runeId":5273},{"count":9,"runeId":5297},{"count":9,"runeId":5317},{"count":3,"runeId":5357},{"count":8,"runeId":5402}],"masteries":[{"rank":4,"masteryId":4112},{"rank":4,"masteryId":4113},{"rank":3,"masteryId":4123},{"rank":1,"masteryId":4131},{"rank":1,"masteryId":4133},{"rank":3,"masteryId":4143},{"rank":3,"masteryId":4152},{"rank":1,"masteryId":4154},{"rank":1,"masteryId":4162},{"rank":1,"masteryId":4211},{"rank":2,"masteryId":4212},{"rank":2,"masteryId":4213},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4232}]},{"teamId":100,"spell1Id":7,"spell2Id":4,"championId":104,"profileIconId":608,"summonerName":"Gunex","bot":false,"summonerId":653768,"runes":[{"count":9,"runeId":5245},{"count":9,"runeId":5290},{"count":9,"runeId":5317},{"count":3,"runeId":5335}],"masteries":[{"rank":1,"masteryId":4111},{"rank":4,"masteryId":4112},{"rank":1,"masteryId":4121},{"rank":3,"masteryId":4122},{"rank":1,"masteryId":4131},{"rank":1,"masteryId":4132},{"rank":3,"masteryId":4134},{"rank":3,"masteryId":4142},{"rank":3,"masteryId":4152},{"rank":1,"masteryId":4162},{"rank":2,"masteryId":4211},{"rank":2,"masteryId":4213},{"rank":1,"masteryId":4221},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4232}]},{"teamId":100,"spell1Id":4,"spell2Id":3,"championId":117,"profileIconId":660,"summonerName":"uunk","bot":false,"**summonerId**":633003,"runes":[{"count":9,"runeId":5273},{"count":9,"runeId":5298},{"count":9,"runeId":5316},{"count":3,"runeId":5357}],"masteries":[{"rank":2,"masteryId":4211},{"rank":2,"masteryId":4212},{"rank":1,"masteryId":4221},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4232},{"rank":1,"masteryId":4311},{"rank":3,"masteryId":4312},{"rank":3,"masteryId":4313},{"rank":3,"masteryId":4322},{"rank":1,"masteryId":4324},{"rank":3,"masteryId":4331},{"rank":1,"masteryId":4334},{"rank":1,"masteryId":4341},{"rank":1,"masteryId":4342},{"rank":3,"masteryId":4353},{"rank":1,"masteryId":4362}]},{"teamId":200,"spell1Id":14,"spell2Id":4,"championId":25,"profileIconId":585,"summonerName":"Zigoto","bot":false,"**summonerId**":458902,"runes":[{"count":9,"runeId":5273},{"count":9,"runeId":5289},{"count":9,"runeId":5317},{"count":3,"runeId":5357}],"masteries":[{"rank":4,"masteryId":4113},{"rank":1,"masteryId":4121},{"rank":3,"masteryId":4123},{"rank":1,"masteryId":4131},{"rank":1,"masteryId":4133},{"rank":3,"masteryId":4134},{"rank":3,"masteryId":4143},{"rank":3,"masteryId":4152},{"rank":1,"masteryId":4154},{"rank":1,"masteryId":4162},{"rank":3,"masteryId":4312},{"rank":1,"masteryId":4313},{"rank":3,"masteryId":4322},{"rank":1,"masteryId":4324},{"rank":1,"masteryId":4334}]},{"teamId":200,"spell1Id":4,"spell2Id":11,"championId":60,"profileIconId":28,"summonerName":"TakeruX","bot":false,"**summonerId**":1513707,"runes":[{"count":9,"runeId":5289},{"count":9,"runeId":5317},{"count":3,"runeId":5357},{"count":9,"runeId":5402}],"masteries":[{"rank":4,"masteryId":4113},{"rank":1,"masteryId":4114},{"rank":3,"masteryId":4123},{"rank":1,"masteryId":4124},{"rank":1,"masteryId":4133},{"rank":3,"masteryId":4134},{"rank":3,"masteryId":4143},{"rank":3,"masteryId":4152},{"rank":1,"masteryId":4154},{"rank":1,"masteryId":4162},{"rank":2,"masteryId":4213},{"rank":2,"masteryId":4214},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4224},{"rank":1,"masteryId":4232}]},{"teamId":200,"spell1Id":4,"spell2Id":12,"championId":150,"profileIconId":518,"summonerName":"52GO Jabiraka","bot":false,"**summonerId**":531086,"runes":[{"count":8,"runeId":5245},{"count":1,"runeId":5251},{"count":9,"runeId":5289},{"count":9,"runeId":5317},{"count":1,"runeId":5335},{"count":2,"runeId":5343}],"masteries":[{"rank":4,"masteryId":4113},{"rank":1,"masteryId":4114},{"rank":3,"masteryId":4122},{"rank":1,"masteryId":4132},{"rank":2,"masteryId":4211},{"rank":2,"masteryId":4213},{"rank":1,"masteryId":4221},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4232},{"rank":3,"masteryId":4233},{"rank":3,"masteryId":4234},{"rank":1,"masteryId":4243},{"rank":1,"masteryId":4244},{"rank":3,"masteryId":4252},{"rank":1,"masteryId":4262}]},{"teamId":200,"spell1Id":4,"spell2Id":7,"championId":18,"profileIconId":718,"summonerName":"Reed x","bot":false,"**summonerId**":891096,"runes":[{"count":9,"runeId":5245},{"count":9,"runeId":5289},{"count":9,"runeId":5317},{"count":3,"runeId":5337}],"masteries":[{"rank":3,"masteryId":4112},{"rank":1,"masteryId":4114},{"rank":3,"masteryId":4122},{"rank":1,"masteryId":4124},{"rank":1,"masteryId":4131},{"rank":1,"masteryId":4132},{"rank":3,"masteryId":4134},{"rank":1,"masteryId":4141},{"rank":1,"masteryId":4142},{"rank":1,"masteryId":4144},{"rank":1,"masteryId":4151},{"rank":3,"masteryId":4152},{"rank":1,"masteryId":4162},{"rank":2,"masteryId":4211},{"rank":2,"masteryId":4212},{"rank":1,"masteryId":4221},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4232}]},{"teamId":200,"spell1Id":4,"spell2Id":3,"championId":267,"profileIconId":610,"summonerName":"SK Sheldon","bot":false,"**summonerId**":2982516,"runes":[{"count":1,"runeId":5251},{"count":8,"runeId":5257},{"count":9,"runeId":5297},{"count":5,"runeId":5315},{"count":4,"runeId":5317},{"count":1,"runeId":5345},{"count":2,"runeId":5357}],"masteries":[{"rank":2,"masteryId":4211},{"rank":2,"masteryId":4212},{"rank":1,"masteryId":4221},{"rank":3,"masteryId":4222},{"rank":1,"masteryId":4232},{"rank":3,"masteryId":4233},{"rank":3,"masteryId":4312},{"rank":3,"masteryId":4313},{"rank":1,"masteryId":4314},{"rank":1,"masteryId":4322},{"rank":1,"masteryId":4324},{"rank":3,"masteryId":4331},{"rank":2,"masteryId":4333},{"rank":1,"masteryId":4334},{"rank":1,"masteryId":4341},{"rank":1,"masteryId":4342},{"rank":1,"masteryId":4352}]}],"observers":{"encryptionKey":"dkGJf37jaGpYGYBhwbBaNcG9XvUKW9WQ"},"platformId":"BR1","bannedChampions":[{"championId":64,"teamId":100,"pickTurn":1},{"championId":238,"teamId":200,"pickTurn":2},{"championId":40,"teamId":100,"pickTurn":3},{"championId":103,"teamId":200,"pickTurn":4},{"championId":268,"teamId":100,"pickTurn":5},{"championId":55,"teamId":200,"pickTurn":6}],"gameStartTime":1423276435804,"gameLength":-126}

I recommend preview .

And within all of this, I need to get all the summonerId , which in this case is 10. This is my code, however it just takes the first summonerId and ignores the rest:

Map json1 = (Map)parser.parse(matei);
JSONObject wtf1 = (JSONObject) json1;
JSONArray eat = (JSONArray) wtf1.get("participants");
JSONObject dash= (JSONObject) eat.iterator().next();
Long wtfbo = (Long) dash.get("summonerId");
System.out.println(wtfbo);

Does anyone have any idea what I can do to get everyone?

    
asked by anonymous 07.02.2015 / 03:51

2 answers

1

I do not know exactly where you got the JSONArray and JSONObject classes, since there are many different projects done independently by many different people who have classes with these names.

However, assuming your code does not generate any compilation errors, it seems to me that you are using Iterator incorrectly, just by taking the first element of it instead of traversing it. So you could do like this:

Map json1 = (Map) parser.parse(matei);
JSONObject wtf1 = (JSONObject) json1;
JSONArray eat = (JSONArray) wtf1.get("participants");
Iterator it = eat.iterator();
while (it.hasNext()) {
    JSONObject dash = (JSONObject) it.next();
    Long wtfbo = (Long) dash.get("summonerId");
    System.out.println(wtfbo);
}
    
07.02.2015 / 04:13
1

The class I use to handle JSON does not have any method iterator , yet it follows an alternative using loop :

JSONArray array = new JSONObject(json).getJSONArray("participants");
int lenght = array.length();

for(int i = 0; i < lenght; i++){
   JSONObject current = array.getJSONObject(i);
   System.out.println(current.get("summonerId")); // faz algo...
}

Testing as input the JSON that put the question together, the output was those numbers .

    
07.02.2015 / 04:40