常见的情况是,我们从mysql读出了数据,批量灌入到HASHMAP里,于是这个HashMap里,有int类型,有string,有boolean,有long
下面的函数,直接判断出数据的类型,方便我们做转义的操作。之前写过一个instanceof 的专门用法。
public static String getType(T t){
if(t instanceof String){
return "string";
}else if(t instanceof Integer){
return "int";
}else if(t instanceof Boolean){
return "boolean";
}else{
return " do not know";
}
}
一般这么用。
Iterator iter = h.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
String k = (String) key;
String v = "";
String t = getType(val);
if(getType(val).equals("int")){
Integer I = (Integer)val;
v = String.valueOf(I.intValue());
}else if("boolean".equals(t)){
Boolean b = (Boolean)val;
if(b) v = "1";
if(!b) v = "0";
}
else{
v = (String) val;
}
sb.append(k + "=" + v + "&");
}
You must be logged in to post a comment.
才了解点JavaScript的知识….