简体   繁体   中英

Extract JSON string from unstructured string

I have an unstructured String and I would like to extract the following JSON string with the "restaurant" tag from there using the regex. The data is for the example but the format and the "restaurant" tag is correct.

{
    "restaurant": {
        "id": "abcd-efgh-ijkl",
        "created_at": "2020-12-31",
        "cashier_payments": []
    }
 }

I come up with the regex String findMe = "\"restaurant\": {(\\n.*?)+}"; , however, its taking all the data till the last } .

How do I correct the regex?

As asked, I get the unstructured String using the Jsoup:

        String htmlString = contentBuilder.toString();
        Document doc = Jsoup.parse(htmlString);
        Elements elements = doc.getElementsByTag("script");
    
        for (Element element :elements ){
            
            for (DataNode node : element.dataNodes()) {
                String s = node.getWholeData();
                if(s.contains("\"restaurant\":")){
                    System.out.println(s);
                }
            }
            System.out.println("-------------------");
        }

So I would like to parse from the String s.

If the entries you're intending to extract do not contain objects (otherwise, you'll need a proper JSON parser), you can use the following regex: "restaurant":\s*\{[^}]*\}
Edit: It seems like the value object does indeed contain other objects, so I'll suggest using a JSON library, like Jackson.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM