mishti
mishti

Reputation: 193

How to get JSON data from HTML using jsoup

I am hitting a URL in WebView which returns a response in HTML format. I have tried to get JSON data from that HTML using Jsoup but not able to get that.

I am getting data in below format :

<pre style="word-wrap: break-word; white-space: pre-wrap;">{"status":"success","connected_id":"dfdffdffdfdfdf"}</pre>

Now I want to get that connected_id from the above response but I am not able to get that.

Code :

Document document = Jsoup.parse(html);
Elements elements = document.select("pre");
Log.d("TAG", " myHTMLResponseCallback1 : " + elements.attr("pre"));

I am not getting any value in elements.attr("connected_id").

Upvotes: 0

Views: 4852

Answers (2)

Eraldo
Eraldo

Reputation: 189

The best way to retrive json data from html with jsoup is extract json through data() method:

Document document = Jsoup.parse(html);
Element element = document.selectFirst("pre#id");
String jsonText = element .data();

Upvotes: 2

Samuel Philipp
Samuel Philipp

Reputation: 11042

So the problem here is that your <pre> element contains a JSON string, which can not be parsed using Jsoup. The first step anyway is to extract the JSON string:

String html = "<pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\"status\":\"success\",\"connected_id\":\"dfdffdffdfdfdf\"}</pre>";
Document document = Jsoup.parse(html);
Element element = document.selectFirst("pre");
String json = element.text();

The simplest, but probably not the best way to extract the connected_id from the JSON string would be using a regex:

Pattern pattern = Pattern.compile("\"connected_id\":\"(?<id>.*)\"");
Matcher matcher = pattern.matcher(json);
if (matcher.find()) {
    String connectedId = matcher.group("id");
    // ...
}

A better way would be to parse the JSON string. You can achieve this using a JSON parsing library like Jackson, Gson or others.

Here is an example using Jackson:

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
String connectedId = jsonNode.get("connected_id").asText();

If you want to extract some more values (not only the connected_id) I would suggest transforming the JSON string to a Java object like this:

public class MyObject {
    private String status;
    @JsonProperty("connected_id")
    private String connectedId;
    // more attributes if you need them

    // getter and setter
}

You now can use this class to read the json value:

ObjectMapper mapper = new ObjectMapper();
MyObject result = mapper.readValue(json, MyObject.class);
String connectedId = result.getConnectedId();

All solutions will return dfdffdffdfdfdf as result.

Upvotes: 1

Related Questions