【问题标题】:How to get a script from html with jsoup in android如何在android中使用jsoup从html获取脚本
【发布时间】:2018-07-28 20:44:05
【问题描述】:

我想使用 jsoup 从 html 中的以下脚本标记中获取“慢炖锅食谱的哈希布朗砂锅”和“PT20M”。我看了一下this,但没有得到明确的解决方案。任何指导表示赞赏。

<script id="ld" type="application/ld+json">{"@context": "http://schema.org/","@type": "Recipe","name": "Hash Brown Casserole for the Slow Cooker Recipe","prepTime":"PT20M"</script>

【问题讨论】:

  • 这里有人吗?!

标签: android jsoup


【解决方案1】:

查看脚本中有 id。您可以通过 id 获取元素,并且有一个孩子。然后您可以将此子对象转换为 json 对象。示例如下:

Document doc = Jsoup.parse("<script id=\"ld\" type=\"application/ld+json\">{\"@context\": \"http://schema.org/\",\"@type\": \"Recipe\",\"name\": \"Hash Brown Casserole for the Slow Cooker Recipe\",\"prepTime\":\"PT20M\"}</script>");
        String str = doc.getElementById("ld").childNodes().get(0).toString();
        JSONObject jsonObject = new JSONObject(str);
        System.out.println(jsonObject.getString("name"));
        System.out.println(jsonObject.getString("prepTime"));

**您的脚本包含: {"@context": "http://schema.org/","@type": "Recipe","name": "Hash Brown Casserole for the Slow Cooker Recipe","prepTime":"PT20M"

如果PT20M 之后没有},则需要连接}

String str = doc.getElementById("ld").childNodes().get(0).toString()+"}";

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多