2014-01-08 62 views
0

我試圖從地圖獲取特定值。如何抓取內部的JSON地圖

我需要分配給 「leaguePoints」, 「層」, 「等級」 值, 「queueType」: 「RANKED_SOLO_5x5」 & & 「playerOrTeamName」: 「Invicterr」。

我在下面嘗試添加代碼。

我想我需要迭代條目:[],但我不知道如何去做到這一點。 我一共是JSON新手,和Java begginer。

地圖:

"32303008": { 
    "queue": "RANKED_SOLO_5x5", 
    "name": "Sejuani's Berserkers", 
    "entries": [ 
    { 
     "isHotStreak": false, 
     "isFreshBlood": false, 
     "leagueName": "Sejuani's Berserkers", 
     "miniSeries": { 
      "progress": "LWN", 
      "target": 2, 
      "losses": 1, 
      "timeLeftToPlayMillis": 0, 
      "wins": 1 
     }, 
     "isVeteran": false, 
     "tier": "PLATINUM", 
     "lastPlayed": 0, 
     "playerOrTeamId": "21747474", 
     "leaguePoints": 100, 
     "rank": "V", 
     "isInactive": false, 
     "queueType": "RANKED_SOLO_5x5", 
     "playerOrTeamName": "sunchasee", 
     "wins": 127 
    }, 
    { 
     "isHotStreak": false, 
     "isFreshBlood": false, 
     "leagueName": "Sejuani's Berserkers", 
     "isVeteran": false, 
     "tier": "PLATINUM", 
     "lastPlayed": 1389198358615, 
     "playerOrTeamId": "32303008", 
     "leaguePoints": 64, 
     "rank": "V", 
     "isInactive": false, 
     "queueType": "RANKED_SOLO_5x5", 
     "playerOrTeamName": "Invicterr", 
     "wins": 462 
    } 
    ], 
    "tier": "PLATINUM" 
} 

我的代碼:

for (Map.Entry<String, League> entry : leagues.entrySet()) 
{ 
try 
{ 
    if(Integer.parseInt(entry.getKey()) == 32303008) 
     { 
      System.out.println(entry.getKey() + "|" + entry.getValue()); 
     } 
} 
catch(Exception e){} 
} 
+0

步驟1)使用其中一個不同的工具包將JSON字符串轉換爲一組嵌套的地圖和數組。步驟2)通過地圖條目「32303008」,地圖條目「條目」,數組元素0,地圖條目「leaguePoints」的「鏈」訪問。等其他值。不需要迭代。 –

回答

0

我會建議使用像JSON Simple。這會將您的JSON轉換爲更簡單的Java對象。

0

僞代碼:

Map jsonMain = JSONParser.parse(myJSONSourceString); 
Map numberedEntry = (Map)(jsonMain.get("3203008")); 
List entries = (List)(numberedEntry.get("entries")); 
for (int i = 0; i < entries.size(); i++) { 
    Map theEntry = (Map)(entries.get(0)); 
    String queueType = (String)(theEntry.get("queueType")); 
    String playerOrTeamName = (String)(theEntry.get("playerOrTeamName")); 
    if (queueType.equals("RANKED_SOLO_5x5") && (playerOrTeamName.equals("Invicterr")) { 
     Number leaguePoints = (Number)(theEntry.get("leaguePoints")); 
     int leaguePointsValue = leaguePoints.intValue(); 
     String tier = (String)(theEntry.get("tier")); 
     String rank = (String)(theEntry.get("rank")); 
     // Do something with the values 
     break; 
    } 
} 

的精確類型和方法的名稱(以及是否需要澆鑄)將取決於JSON試劑盒使用。