繁体   English   中英

如何从同一个JSONObject获取多个字符串?

[英]How to get multiple strings from same JSONObject?

我有那个JSON代码( http://pastebin.com/TjjJ4jWk ),他有10个召唤者ID

(格式化版本)

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

当我运行它时,他返回(注意这些值在一个Long变量“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:抱歉英文不好

假设您需要处理任意数字,则需要使用Collection。 您没有说明任何特定要求,因此我假设您只是想按照它们在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.

如果您确实需要通过“a”,“b”等访问,则可以使用Map实现此目的

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM