如何迭代String of String,String的集合。
我有一个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;
}
现在我将String转换为gson并尝试将数据映射到pojo类。
Gson gson = new Gson();
GPObject_PublicUser hello =gson.fromJson( jsonText, GPObject_PublicUser.class );
示例json text:
{ "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. 'Americas Now' 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 :(得分:1)
您可以使用高级for循环获取entrySet()
并进行迭代。 Map.Entry
类在地图中代表一对,它有两个属性key
和value
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();
}
}
答案 1 :(得分:0)
要迭代地图集合,您可以使用它:
for(Map<String, String> map : placesLived) {
for(Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
}
希望这有帮助
答案 2 :(得分: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集合,它们是我们最好的武器之一;)