一直在想着如何能写出一个正则表达式来判断一个字符串是否为json数组,想来想去没想出办法,先进行第一步,判断其是被{}或[]包裹吧,测试代码如下:
Pattern pattern = Pattern.compile("(^\\[.*\\]$|^\\{.*\\}$){1}"); String s1 = "[aaa]"; //true String s2 = "[aaa"; String s3 = "aaa]"; String s4 = "{aaa}";//true String s5 = "{aaa"; String s6 = "aaa}"; String s7 = "aaa"; System.out.println(pattern.matcher(s1).matches()); System.out.println(pattern.matcher(s2).matches()); System.out.println(pattern.matcher(s3).matches()); System.out.println(pattern.matcher(s4).matches()); System.out.println(pattern.matcher(s5).matches()); System.out.println(pattern.matcher(s6).matches()); System.out.println(pattern.matcher(s7).matches());
希望尽快找到办法.