字符串格式如下:
area:0,price:4500,serviceFee:300;area:2452,price:4800,serviceFee:300
如果使用解析字符串,按位或是按字符拆分的话,容易出错;
换一个中更兼容的方式进行解析,最好是以键值对的方式进行解析,存入对象属性中,
求解?请各位帮忙解答,多谢
字符串格式如下:
area:0,price:4500,serviceFee:300;area:2452,price:4800,serviceFee:300
如果使用解析字符串,按位或是按字符拆分的话,容易出错;
换一个中更兼容的方式进行解析,最好是以键值对的方式进行解析,存入对象属性中,
求解?请各位帮忙解答,多谢
转换成json,然后用json工具解析出来,放入到map中
public static void main(String[] args) {
String str = "area:0,price:4500,serviceFee:300;area:2452,price:4800,serviceFee:300";
String[] split = str.split(";");
for (String s : split) {
s = s.replaceAll(":","\":\"");
s = s.replaceAll(",","\",\"");
s = "{\""+s+"\"}";
HashMap map = JSONObject.parseObject(s, HashMap.class);
System.out.println(map.get("area"));
System.out.println(map.get("price"));
System.out.println(map.get("serviceFee"));
}
}