欢迎光临
我们一直在努力

java使用正则表达式的方法从json格式的字符串中提取需要的字符串

要解析的json字符串:

 {
    "ROOT": {
        "HEADER": {
            "ROUTING": {
                "ROUTE_KEY": "",
                "ROUTE_VALUE": ""
            }
        },
        "BODY": {
            "OUT_DATA": {
                "TABLES": [{
                    "TABLENAME": "BS_FUNCTION_DICT",
                    "DATARECORDS": [{
                        "COLS": {
                            "FUNCTION_CODE": "2923",
                            "FUNCTION_NAME": "权限信息管理" 
                        },
                        "OP": "I" 
                    }]
                }]
            }
        }
    }
}

实现代码如下:

String content = " {\n" +
                "\t\"ROOT\": {\n" +
                "\t\t\"HEADER\": {\n" +
                "\t\t\t\"ROUTING\": {\n" +
                "\t\t\t\t\"ROUTE_KEY\": \"\",\n" +
                "\t\t\t\t\"ROUTE_VALUE\": \"\"\n" +
                "\t\t\t}\n" +
                "\t\t},\n" +
                "\t\t\"BODY\": {\n" +
                "\t\t\t\"OUT_DATA\": {\n" +
                "\t\t\t\t\"TABLES\": [{\n" +
                "\t\t\t\t\t\"TABLENAME\": \"BS_FUNCTION_DICT\", \n" +
                "\t\t\t\t\t\"DATARECORDS\": [{\n" +
                "\t\t\t\t\t\t\"COLS\": {\n" +
                "\t\t\t\t\t\t\t\"FUNCTION_CODE\": \"2923\",\n" +
                "\t\t\t\t\t\t\t\"FUNCTION_NAME\": \"权限信息管理\"  \n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"OP\": \"I\" \n" +
                "\t\t\t\t\t}]\n" +
                "\t\t\t\t}]\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";

        String regex = "\"TABLENAME\": \"(.*?)\",";
        Matcher matcher = Pattern.compile(regex).matcher(content);
        if (matcher.find()) {
            String tableName = matcher.group(1);
            System.out.println(tableName);

        }

参考:

1.http://blog.csdn.net/yangruihong/article/details/17245359;

赞(0)
未经允许不得转载:Ddmit » java使用正则表达式的方法从json格式的字符串中提取需要的字符串

评论 抢沙发

登录

找回密码

注册