【问题标题】:I want to extract the content of <span> tag from this received JSON Response. How to do that?我想从收到的 JSON 响应中提取 <span> 标记的内容。怎么做?
【发布时间】:2020-11-22 07:26:16
【问题描述】:

这是我得到的回应。我想从price_html中提取&lt;span&gt;标签

        "id": 2320,
        "name": "Lakme Sun Expert Sunscreen (spf-50) 50ml",
        "slug": "lakme-sun-expert-sunscreen-spf-50-50ml",
        "permalink": "https://www.utkalmerchandise.com/product/lakme-sun-expert-sunscreen-spf-50-50ml/",
        "date_created": "2020-08-01T05:49:57",
        "date_created_gmt": "2020-08-01T05:49:57",
        "date_modified": "2020-08-01T05:49:57",
        "date_modified_gmt": "2020-08-01T05:49:57",
        "type": "variable",
        "status": "publish",
        "featured": false,
        "catalog_visibility": "visible",
        "description": "",
        "short_description": "",
        "sku": "",
        "price": "95",
        "regular_price": "",
        "sale_price": "",
        "date_on_sale_from": null,
        "date_on_sale_from_gmt": null,
        "date_on_sale_to": null,
        "date_on_sale_to_gmt": null,
        "price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#8377;</span>95.00</span> &ndash; <span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#8377;</span>219.00</span>",```

【问题讨论】:

    标签: android html json retrofit android-volley


    【解决方案1】:

    只引用对象

    var myObj={"id": 2320,
            "name": "Lakme Sun Expert Sunscreen (spf-50) 50ml",
            "slug": "lakme-sun-expert-sunscreen-spf-50-50ml",
            "permalink": "https://www.utkalmerchandise.com/product/lakme-sun-expert-sunscreen-spf-50-50ml/",
            "date_created": "2020-08-01T05:49:57",
            "date_created_gmt": "2020-08-01T05:49:57",
            "date_modified": "2020-08-01T05:49:57",
            "date_modified_gmt": "2020-08-01T05:49:57",
            "type": "variable",
            "status": "publish",
            "featured": false,
            "catalog_visibility": "visible",
            "description": "",
            "short_description": "",
            "sku": "",
            "price": "95",
            "regular_price": "",
            "sale_price": "",
            "date_on_sale_from": null,
            "date_on_sale_from_gmt": null,
            "date_on_sale_to": null,
            "date_on_sale_to_gmt": null,
            "price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#8377;</span>95.00</span> &ndash; <span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#8377;</span>219.00</span>"};
            
            console.log(myObj['price_html']);

    【讨论】:

      【解决方案2】:

      如果你有它作为一个字符串,你可以使用

      string.indexOf("<span class="...">")
      

      这将返回搜索字符串中第一个字符的索引。然后将搜索字符串的长度添加到该索引并保存到 index。然后你可以string.indexOf("&lt;/", index) 来获取内容的结尾。通过string.subString(indexStart, indexEnd),您可以获得内容。

      编辑:如果您无法使用它,请查看 org.json 包,该包在使用 java 和 json 时非常有用。

      【讨论】:

        猜你喜欢
        • 2023-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-15
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        • 2017-03-13
        相关资源
        最近更新 更多