我能夠以這種格式打印我的輸出System.out.println(map.get("email"));//this is printing fine
但我無法在將它分配給字符串變量後輸出相同的值。我試過了:String email=(String) map.get("email"); System.out.println("Email--"+email);//But this is not printing
我如何將地圖值轉換爲字符串?請幫幫我。如何將映射值轉換爲Java中的字符串
我全碼:
String url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="
+ authResponse.accessToken;
final StringBuffer r = new StringBuffer();
final URL u = new URL(url);
final URLConnection uc = u.openConnection();
final int end = 1000;
InputStreamReader isr = null;
BufferedReader br = null;
isr = new InputStreamReader(uc.getInputStream());
br = new BufferedReader(isr);
final int chk = 0;
String pat = "\"(.*)\": \"(.*)\",";
Pattern pattern = Pattern.compile(pat);
Matcher matcher = null;
Map map = new HashMap();
while ((url = br.readLine()) != null)
{
if ((chk >= 0) && ((chk < end))) {
matcher = pattern.matcher(url);
if(matcher.find()) {
map.put(matcher.group(1), matcher.group(2));
}
//r.append(url).append('\n');
}
}
System.out.println(map.get("email"));
String email=(String) map.get("email");
System.out.println(email);
+1提出了這個正確的解決方案,而不只是固定代碼 –