zoukankan      html  css  js  c++  java
  • 提取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
    
    
    

      



  • 相关阅读:
    常用作图与图片处理工具
    在博客园中配置MathJax
    Make Eudict for reviewing example sentences
    使用AltSearch格式化Kindle读书笔记
    Deep Learning基础--理解LSTM/RNN中的Attention机制
    Deep Learning基础--理解LSTM网络
    Deep Learning基础--SVD奇异值分解
    常见面试之机器学习算法思想简单梳理
    Deep Learning基础--CNN的反向求导及练习
    Deep Learning基础--线性解码器、卷积、池化
  • 原文地址:https://www.cnblogs.com/ysq0908/p/11574833.html
Copyright © 2011-2022 走看看