获取JSON模式的Java正则表达式

获取JSON模式的Java正则表达式,java,regex,json,Java,Regex,Json,我需要从字符串中获取JSON对象 在该字符串中,JSON内容与以下形式是唯一的: abit2:{……} 我这样做了我的正则表达式,但我不能停在分号处 public String getJSON(String content) throws MalformedURLException, IOException{ String json = ""; String regex = "abit2:([\\s\\S]*)\\};"; Pattern patter

我需要从字符串中获取JSON对象

在该字符串中,JSON内容与以下形式是唯一的:

abit2:{……}

我这样做了我的正则表达式,但我不能停在分号处

public String getJSON(String content) throws MalformedURLException, IOException{

    String json = "";       

    String regex = "abit2:([\\s\\S]*)\\};"; 
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(content);     
    while (matcher.find()) {            
        json = matcher.group().replace("abit2:", "");       
    }

    return json;        
}

您忘了包含开头的花括号,需要将此
[\\s\\s]*
模式设置为非贪婪模式

String regex = "abit2:\\{([\\s\\S]*?)\\};";

不要这样做。Java中有一些著名的JSON解析器库。