Html, handling a JSON response
Asked Answered
U

2

13

I have a page that comes back as an UnexpectedPage in HtmlUnit, the response is JSON. Can I use HTMLUnit to parse this or will I need an additional library?

Unsparing answered 28/5, 2010 at 22:15 Comment(0)
G
21

HtmlUnit doesn't support it. It can at highest execute a JS function. You need to check beforehand if the Content-Type of the returned response matches application/json and then use the suitable tool to parse it. Google Gson is useful in this.

WebClient client = new WebClient();
Page page = client.getPage("https://stackoverflow.com/users/flair/97901.json");
WebResponse response = page.getWebResponse();
if (response.getContentType().equals("application/json")) {
    String json = response.getContentAsString();
    Map<String, String> map = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {}.getType());
    System.out.println(map.get("displayName")); // Benju
}

If the JSON structure is known beforehand, you can even use Gson to convert it to a fullworthy Javabean. You can find an example in this answer.

Gog answered 28/5, 2010 at 23:31 Comment(0)
A
0

BalusC provided a good answer, but to answer the literal question, you don't really need an additional library: you can use Groovy's neat built-in JsonSlurper, e.g:

 def jsonSlurper = new groovy.json.JsonSlurper()
 def parsed = jsonSlurper.parseText(response.getContentAsString())
 println("Found ${parsed.TotalCount} records.");

to print out 1 for a response such as

'{"Records":[{"ID":"123","Address":"Zagreb",],"TotalCount":1}'
Andonis answered 11/10, 2020 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.