1

I need to get a JSON object from a String

in this string the JSON content is unique with this form:

abit2:{....};

I made my regex this way, but I'm unable to stop at semicolon

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;        
}
1
  • 3
    Don't do this. There are some well known JSON parser libraries in Java. Commented Feb 21, 2015 at 9:22

1 Answer 1

1

You forget to include the opening curly brace and you need to make this [\\s\\S]* pattern as non-greedy.

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.