简体   繁体   English

如何使用 jsoup 从 HTML 中获取 JSON 数据

[英]How to get JSON data from HTML using jsoup

I am hitting a URL in WebView which returns a response in HTML format.我在 WebView 中点击了一个 URL,它以 HTML 格式返回响应。 I have tried to get JSON data from that HTML using Jsoup but not able to get that.我曾尝试使用 Jsoup 从该 HTML 中获取 JSON 数据,但无法获取。

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.现在我想从上面的响应中得到那个connected_id但我无法得到它。

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") .我在elements.attr("connected_id")没有得到任何值。

The best way to retrive json data from html with jsoup is extract json through data() method:使用 jsoup 从 html 检索 json 数据的最佳方法是通过 data() 方法提取 json:

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

So the problem here is that your <pre> element contains a JSON string, which can not be parsed using Jsoup.所以这里的问题是你的<pre>元素包含一个 JSON 字符串,无法使用 Jsoup 解析。 The first step anyway is to extract the JSON string:无论如何,第一步是提取 JSON 字符串:

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:从 JSON 字符串中提取connected_id的最简单但可能不是最好的方法是使用正则表达式:

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.更好的方法是解析 JSON 字符串。 You can achieve this using a JSON parsing library like Jackson , Gson or others .您可以使用 JSON 解析库(如JacksonGson其他)来实现这一点。

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:如果您想提取更多值(不仅是connected_id ),我建议将 JSON 字符串转换为这样的 Java 对象:

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:您现在可以使用这个类来读取 json 值:

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

All solutions will return dfdffdffdfdfdf as result.所有解决方案都将返回dfdffdffdfdfdf作为结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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