2015-06-12 40 views
1

我試圖將所有值都放入JSONArray stop中的3個映射directionMap. directionMap2, directionMap3中。在這些地圖中,所有鍵name(stop_name)是相同的,但值timeEntries, timeEntries2, timeEntries3(星期一,星期六,星期六,星期五)是不同的,所以我想把所有這些值放在同一個JSONObject arrivals但我面臨問題循環所有這些3個地圖將同一個鍵的所有值集中在一起?循環遍歷3個映射以獲取相同密鑰的值

我試圖把3循環他們在對方,但它不是正確的解決方案。

我很感激任何幫助。

結果應該爲這個簡單:

[ 
    { 
     "arrival_time": {"mon-fri": ["04:24","05:10","05:40"], 
         "sat": ["05:34","05:55","06:15"], 
         "son": ["07:00","08:00","05:40"] 

         } 
     "stop_name": "garden street" 
    }, 
    { 
     "arrival_time": {"mon-fri": ["04:24","05:10","05:40"], 
         "sat": ["05:34","05:55","06:15"], 
         "son": ["07:00","08:00","05:40"] 

         } 
     "stop_name": "luise street" 
    } 
] 

代碼:

JSONArray stops = new JSONArray(); 

     for (int index = 0; index < mainList.size(); index += 3) { 

      RouteCreator route_mon_fri = mainList.get(index); 
      Map<String, List<String>> directionMap = route_mon_fri 
        .getDirectionMap(); 
      String direction = route_mon_fri.getDirection(); 
      String route = route_mon_fri.getRoute(); 

      RouteCreator route_sat = mainList.get(index + 1); 
      Map<String, List<String>> directionMap2 = route_sat 
        .getDirectionMap(); 

      RouteCreator route_son = mainList.get(index + 2); 
      Map<String, List<String>> directionMap3 = route_son 
        .getDirectionMap(); 

      List<String> timeEntries; 
      for (Entry<String, List<String>> entry : directionMap.entrySet()) { 
       String name = entry.getKey().trim(); 
       timeEntries = entry.getValue(); 

       try { 
        JSONObject stop = new JSONObject(); 

        JSONObject arrivals = new JSONObject(); 
        JSONArray timeArray = new JSONArray(timeEntries); 
        //JSONArray timeArray2 = new JSONArray(timeEntries2); 
        //JSONArray timeArray3 = new JSONArray(timeEntries3); 
        arrivals.put("mon-fri", timeArray); 
        //arrivals.put("sat", timeArray2); 
        //arrivals.put("son", timeArray3); 

        stop.put("arrival_time", arrivals); 
        stop.put("stop_name", name); 
        stops.put(stop); 
        System.out.println(stops.toString(3)); 

       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
      List<String> timeEntries2; 
      for (Entry<String, List<String>> entry2 : directionMap2.entrySet()) { 
       timeEntries2 = entry2.getValue(); 
      } 
      List<String> timeEntries3; 
      for (Entry<String, List<String>> entry3 : directionMap3.entrySet()) { 
       timeEntries3 = entry3.getValue(); 

      } 


} 
+0

上午我正確地說:directionMap conta從停車名稱到週一至週五的到達時間,directionMap2包含從停車名稱到週六到達時間的地圖,directionMap3包含從停車名稱到太陽到達時間的地圖,並且它們全部具有相同的條目數量? –

+0

@TonyVu:是的,你說得對 – TheBook

回答

1

這個替換您的部分從開始for循環:

JSONArray root = new JSONArray(); 
for (Entry<String, List<String>> entry : directionMap.entrySet()) { 
    JSONObject stop = new JSONObject(); 
    String stopName = entry.getKey(); 
    stop.put("stop_name", stopName); 

    JSONObject arrivalTime = new JSONObject(); 
    JSONArray monFriArrivalTime = new JSONArray(); 
    JSONArray satArrivalTime = new JSONArray(); 
    JSONArray sunFriArrivalTime = new JSONArray(); 
    for (String str: entry.getValue()) { 
     monFriArrivalTime.add(str); 
    } 
    for (String str: directionMap2.get(stopName)) { 
     satArrivalTime.add(str); 
    } 
    for (String str: directionMap3.get(stopName)) { 
     sunFriArrivalTime.add(str); 
    } 
    arrivalTime.put("mon-fri", monFriArrivalTime); 
    arrivalTime.put("sat", satArrivalTime); 
    arrivalTime.put("sun", sunFriArrivalTime); 
    stop.put("arrival_time", arrivalTime);   
    root.add(e); 
} 
System.out.println(root); 
+0

感謝它現在正常工作:) – TheBook