• 提取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
    
    
    

      



  • 相关阅读:
    Unity3D Editor模式下批量修改prefab
    3D touch在Unity3D中的使用
    Unity中的协程是什么?
    Unity3D脚本调用Objective C代码实现游戏内购买
    WindowsPhone8拍照功能实现简介
    WindowsPhone App如何扩展能够使用的内存
    SVN 提交代码时提示文件已经存在解决办法
    iOS检查App新版本并更新新版本
    iOS存储数据字典到沙盒
    统计整个Xcode工程代码行数
  • 原文地址:https://www.cnblogs.com/ysq0908/p/11574833.html
Copyright © 2020-2023  润新知