java把类似a=1&b=2&c=3的String类型数据转成map集合

 public static Map<String, Object> transStringToMap(String mapString, String separator, String pairSeparator) {
Map<String, Object> map = new HashMap<String, Object>();
String[] fSplit = mapString.split(separator);
for (int i = 0; i < fSplit.length; i++) {
if (fSplit[i]==null||fSplit[i].length()==0) {
continue;
}
String[] sSplit = fSplit[i].split(pairSeparator);
String value = fSplit[i].substring(fSplit[i].indexOf('=') + 1, fSplit[i].length());
map.put(sSplit[0], value);
} return map;
}
//把String类型的字符串转成map集合,再遍历出来
public static void main(String[] args) {
String str = "RespCode=F0000&Plain=RespCode=F0000~~RespMsg=成功~~TradeResult=0&Signature=05bd95572c29726e6ff2f2b8f4879889948af05da9c656a8e3b799a8097bd260158e823e4e3a8dcf723189c09816ede5f82d8681abbf18bb8bab023949f766df004e09385b7c62fdb792385d45dbbf4bc7b77b1f9552af3dc023d362742e009207e3ef5e33e57f66dce1d10e28a3a55ad9bde1c7d25f6fa96a8a0449";
Map<String, Object> map = StringUtil.transStringToMap(str, "&", "=");
for (String key : map.keySet()) {
System.out.println("key:" + key + ", value:" + map.get(key));
} }
上一篇:mysql获取随机数据的方法


下一篇:Andy's First Dictionary