0

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").

1
  • What is the value of elements? Commented May 10, 2019 at 9:38

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.