2014-07-19 65 views
-2

如何迭代String,String的地图集合。迭代java中的地图集合

我有一个POJO类

 public class GPObject_PublicUser { 

    private String kind; 
    private String id; 
    private String displayName; 
    private String objectType; 
    private String gender; 
    private String aboutMe; 
    private String url; 


    private Map<String,String> image; 

    private Collection<Map<String,String>> placesLived; 



} 

现在我转换字符串GSON并尝试将数据映射到POJO类。

  Gson gson = new Gson(); 
      GPObject_PublicUser hello =gson.fromJson(jsonText, GPObject_PublicUser.class); 

样品JSON文本:

 { "kind": "plus#person", "etag": "\"goGJzLGpDAdTIjyZUs7et8jwqfg/dSQkZzjp1Ufk7Fq69sg0cCLCwLY\"", "occupation": "Global News Broadcaster", "gender": "other", "urls": [ { "value": "http://twitter.com/cctv_america", "type": "otherProfile", "label": "CCTV_America" }, { "value": "https://www.facebook.com/CCTVAmerica", "type": "otherProfile", "label": "Facebook" }, { "value": "http://www.cctv-america.com", "type": "contributor", "label": "CCTV-America.com" }, { "value": "http://www.cctv-america.com", "type": "other", "label": "Official Website" } ], "objectType": "person", "id": "105608978310637089588", "displayName": "CCTV America", "name": { "familyName": "America", "givenName": "CCTV" }, "tagline": "CCTV America contributes programming from Washington DC to CCTV News, the 24-hour English-language news channel of China Central Television", "aboutMe": "\u003cdiv\u003eCCTV America launched from Washington on Monday, February 6th 2012.  It currently produces five hours of weekday programming seen on the North American east coast in the early evening hours.  Programs include numerous daily news broadcasts as well as BIZ ASIA AMERICA, focusing on global economic trends, trade and investment.\u003c/div\u003e\u003cdiv\u003e“The Heat,� weekdays at 7pm EST provides talk-and-debate on a range of topical issues. &#39;Americas Now&#39; on Sunday nights provides in depth magazine coverage on Central and South American issues.  It is the only regularly scheduled magazine program in the English language exploring Latin American issues.\u003c/div\u003e\u003cdiv\u003e\u003cbr /\u003e\u003c/div\u003e\u003cdiv\u003eCCTV America programming can be seen from 3pm EST\u003c/div\u003e\u003cdiv\u003eCOMCAST Channel 273, FIOS 277 in the greater Washington area.\u003c/div\u003e\u003cdiv\u003eIn New York watch Time Warner Channel 134.\u003c/div\u003e\u003cdiv\u003eIn Los Angeles watch Charter Cable Channel 562.\u003c/div\u003e\u003cdiv\u003eNationwide watch DISH TV channel 279.\u003c/div\u003e\u003cdiv\u003eCheck your cable TV listings for availability in different US locations.\u003c/div\u003e\u003cdiv\u003eExcerpts of all programming can be seen on \u003ca href=\"http://www.cctv-america.com\" rel=\"nofollow\" target=\"_blank\"\u003ewww.cctv-america.com\u003c/a\u003e.\u003c/div\u003e\u003cdiv\u003e\u003cbr /\u003e\u003c/div\u003e", "url": "https://plus.google.com/105608978310637089588", "image": { "url": "https://lh4.googleusercontent.com/-xiA6h0u4IO8/AAAAAAAAAAI/AAAAAAAAAMQ/Hv4BxXfO-_Q/photo.jpg?sz=50", "isDefault": false }, "organizations": [ { "name": "CCTV America", "type": "work", "startDate": "2012", "primary": true } ], "placesLived": [ { "value": "Washington, D.C., DC 20002, United States", "primary": true } ], "isPlusUser": true, "circledByCount": 101, "verified": false, "cover": { "layout": "banner", "coverPhoto": { "url": "https://lh3.googleusercontent.com/-c5Kov6t3y2o/U2KUhNrAClI/AAAAAAAAAME/RZEIUxBcMnk/s630-fcrop64=1,3ceb2465cd58ffff/facebook_banner.png", "height": 347, "width": 940 }, "coverInfo": { "topImageOffset": 0, "leftImageOffset": 0 } }} 

现在我的问题是我怎么能遍历的placeLived来获取值。

+0

参见http://stackoverflow.com/questions/46898/how-do-i -ite-over-each-entry-in-a-map – Raedwald

+0

我认为,即使粗略阅读Java引用,这也应该是显而易见的。这真的值得SO贴吗? – balajeerc

+0

另请参阅http://stackoverflow.com/questions/20654289/iterate-through-collectionlistinteger – Raedwald

回答

0

我只是检查Google's javadoc退货类型为GPObject_PublicUser.placesLived

Iterator<Map<String, String>> iterator = placesLived.iterator(); 

while (iterator.hasNext()) { 
    Map<String, String> place = iterator.next(); 
     for (Map.Entry<String, String> entry : place.entrySet()) { 
      String placeVar1 = entry.getKey(); 
      String placeVar2 = entry.getValue(); 
      // TODO 
     } 
    } 
} 

作为一个温和的建议 - 做Java集合注意,他们是我们最好的武器之一;)

1

你可以得到entrySet()并迭代使用先进的for循环。 Map.Entry类表示地图一对,它有两个属性keyvalue

for(Map<String, String> map : placesLived){ 
    Set<Map.Entry<String, String>> entrySet = map.entrySet();  
    for (Map.Entry<String, String> entry : entrySet) { 
     String key = entry.getKey(); 
     String value = entry.getValue(); 
    } 
} 
0

要遍历图的集合,你可以使用此:

for(Map<String, String> map : placesLived) { 
    for(Map.Entry<String, String> entry : map.entrySet()) { 
     String key = entry.getKey(); 
     String value = entry.getValue(); 

    } 
} 

希望这有助于