2015-03-05 2 views
0

У меня есть, что JSON-код (http://pastebin.com/TjjJ4jWk), он имеет 10 summonerIdКак получить несколько строк из одного JSONObject?

(форматированный версия)

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

и мне нужно, чтобы получить все из них, но теперь они приходят все в одной переменной ("wtfbo «).

@SuppressWarnings("rawtypes") 
    Map json1 = (Map) parser.parse(matei); 
    JSONObject wtf1 = (JSONObject) json1; 
    Long kiwi = (Long) wtf1.get("gameQueueConfigId"); 
    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); 
    } 

Когда я бегу, что он возвращает (Обратите внимание, что эти значения находятся в «wtfbo» одной длинной переменной)

2833743 
2883696 
1356728 
653768 
633003 
458902 
1513707 
531086 
891096 
2982516 

Но мне нужно, чтобы каждый приходит в строке/переменной разъединены. Использование других слов, мне нужно, что

Long a = 2833743 
Long b = 2883696 
Long c = 1356728 
Long d = 653768 
Long e = 633003 
Long f = 458902 
Long g = 1513707 
Long h = 531086 
Long i = 891096 
Long j = 2982516 

Ps: Извините за плохой английский

+0

хранить их в коллекции. Вы идете по этому (типичному) неправильному пути. –

+0

Как вы к ним обращаетесь? По порядку они встречаются в JSON? или другим идентификатором? – Adam

+0

Примечание: эти значения не все существуют в одной длинной переменной одновременно. Одно из этих значений всегда находится в переменной сразу и перезаписывается новым единственным значением на следующей итерации цикла. Тем не менее, предложения по использованию коллекции здесь будут вашим лучшим способом. –

ответ

0

Предполагая, что вы должны иметь дело с произвольным числом, вам нужно использовать коллекцию. Вы не указали каких-либо особых требований, поэтому я предполагаю, что вы просто хотите получить к ним доступ в том порядке, в котором они происходят в JSON.

List<Long> summonerIds = new ArrayList<Long>(); // <== list to store summonerIds 
while (it.hasNext()) { 
    JSONObject dash = (JSONObject) it.next(); 
    summonerIds.add((Long) dash.get("summonerId")); // <== save 
} 

System.out.println(summonerIds.get(0)); // 1st <== access later 
System.out.println(summonerIds.get(2)); // 3rd etc. 

Если вам действительно нужно, чтобы получить доступ на «а», «б» и т.д., вы можете добиться этого с карты

Map<String, Long> summonerIds = new HashMap<String, Long>(); 
int i = 0; 
while (it.hasNext()) { 
    JSONObject dash = (JSONObject) it.next(); 
    summonerIds.put(Character.toString((char) ('a' + i)), (Long) dash.get("summonerId")); 
} 

System.out.println(summonerIds.get("a")); // 1st 
System.out.println(summonerIds.get("c")); // 3rd etc. 
Смежные вопросы