提取json字符串中指定格式中的参数值
直接上代码:
import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestDemo { public static void main(String[] args) {
ArrayList json = new ArrayList(); json.add("111"); json.add("222"); json.add("333"); User user = new User(); user.setJson(json); user.setCode("200"); user.setMessage("true"); String string = "(" + JSON.toJSONString(user) + ")"; System.out.println(string); // String regex = "\"json\":(.*?),\"message\"";// 使用非贪婪模式! Pattern pattern = Pattern.compile("\\[\"(.*?)\"\\]"); Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println(matcher.group(1)); } String parse = "[{CSTM_NO:\"11118\",CSTM_NAME:\"广东XX电力有限公司\",FIX_GNL:\"111810158\",FIX_ACC:\"D201306070003\"," + "OP_FLAG:\"正常\",BUSS_KIND_NAME:\"三个月定期存款\",BAL:\"25,178,750.00\",AVAL_BAL:\"25,178,750.00\"," + "OP_DATE:\"2013-06-07\",DUE_DATE:\"2013-12-07\",ROWNUM_:1,idx:0},{CSTM_NO:\"11118\"," + "CSTM_NAME:\"广东XX电力有限公司\",FIX_GNL:\"111810158\",FIX_ACC:\"D201306070002\",OP_FLAG:\"正常\"," + "BUSS_KIND_NAME:\"三个月定期存款\",BAL:\"25,178,750.00\",AVAL_BAL:\"25,178,750.00\",OP_DATE:\"2013-06-07\"," + "DUE_DATE:\"2013-12-07\",ROWNUM_:3,idx:2},{CSTM_NO:\"11118\",CSTM_NAME:\"广东XX电力有限公司\"," + "FIX_GNL:\"111810158\",FIX_ACC:\"D201306070005\",OP_FLAG:\"正常\",BUSS_KIND_NAME:\"三个月定期存款\"," + "BAL:\"25,178,750.00\",AVAL_BAL:\"25,178,750.00\",OP_DATE:\"2013-06-07\",DUE_DATE:\"2013-12-07\"," + "ROWNUM_:4,idx:3}]"; String regex = "FIX_ACC:(.*?),OP_FLAG";//别忘了使用非贪婪模式! Matcher matcher1 = Pattern.compile(regex).matcher(parse); while (matcher1.find()) { String ret = matcher1.group(1); System.out.println(ret); }
String str = "我是[001]真心求救的[002],你能帮帮我吗"; Pattern pattern1 = Pattern.compile("\\[(.*?)\\]"); Matcher matcher2 = pattern1.matcher(str); while(matcher2.find()){ System.out.println(matcher2.group(1)); } } }
打印结果如下:
({"code":"200","json":["111","222","333"],"message":"true"}) 111","222","333 "D201306070003" "D201306070002" "D201306070005" 001 002 Process finished with exit code 0
生活赋予我们一种巨大的和无限高贵的礼品,这就是青春:充满着力量,充满着期待志愿,充满着求知和斗争的志向,充满着希望信心和青春。