java把类似a=1&b=2&c=3的String类型数据转成map集合
1 public static Map<String, Object> transStringToMap(String mapString, String separator, String pairSeparator) { 2 Map<String, Object> map = new HashMap<String, Object>(); 3 String[] fSplit = mapString.split(separator); 4 for (int i = 0; i < fSplit.length; i++) { 5 if (fSplit[i]==null||fSplit[i].length()==0) { 6 continue; 7 } 8 String[] sSplit = fSplit[i].split(pairSeparator); 9 String value = fSplit[i].substring(fSplit[i].indexOf('=') + 1, fSplit[i].length()); 10 map.put(sSplit[0], value); 11 } 12 13 return map; 14 }
//把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)); } }