要解析的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;