Map<String, String> fieldMap = new HashMap<String, String>();
String strTest1 = "Q1 = '1PAaEaO5Js+zUErWe1Ns8fR==' and q2 = 'v21PAaEaO5Js+zUErWe1Ns8fR='";
String strTest2 = "Q1='1PAaEaO5Js+zUErWe1Ns8fR==' and q2='v21PAaEaO5Js+zUErWe1Ns8fR='";
String[] output = input.split("\\s+and\\s+");
for(String s:output ){
String [] params = s.split("=");
String key = params[0].trim();
String value = params[1].trim();
fieldMap.put(key, value.substring(1, value.length()-1));
}
System.out.println(fieldMap);
我已经尝试过上面的例子,它适用于strTest1。 但是,它没有解析strTest2。
预期输出:{Q1 = 1PAaEaO5Js + zUErWe1Ns8fR ==, Q2 = v21PAaEaO5Js + zUErWe1Ns8fR =}
因为我们正在分裂=然而它不应该从值
中消除由于
答案 0 :(得分:0)
你将每个'='分开,你有3个会搞砸你的进一步解析。
Map<String, String> fieldMap = new HashMap<String, String>();
String strTest1 = "Q1 = '1PAaEaO5Js+zUErWe1Ns8fR==' and q2 = 'v21PAaEaO5Js+zUErWe1Ns8fR='";
String strTest2 = "Q2='1PAaEaO5Js+zUErWe1Ns8fR==' and q3='v21PAaEaO5Js+zUErWe1Ns8fR='";
String[] lines={strTest1,strTest2};
for (String input:lines){
String[] output = input.split("\\s+and\\s+");
for(String s:output ){
String [] params = s.split("=",2);
String key = params[0].trim();
String value = params[1].trim();
fieldMap.put(key, value.substring(1, value.length()-1));
}
}
System.out.println(fieldMap);