Пропустить HTML-теги в ответе JSON открытых API-интерфейсов walmart

Как мы можем получить ответ без экранированных тегов HTML из открытых API Walmart

Пример: при использовании API поиска пример ответа

{
    "query": "ipod",
    "sort": "relevance",
    "responseGroup": "base",
    "totalResults": 257,
    "start": 1,
    "numItems": 10,
    "items": [{
        "itemId": 42608125,
        "parentItemId": 42608125,
        "name": "Apple iPod touch 32GB",
        "msrp": 247.0,
        "upc": "888462353151",
        "categoryPath": "Electronics/Portable Audio/Apple iPods/iPod Touch",
        "shortDescription": "The Apple iPod Touch 32GB is loaded with features. It is the ideal solution for carrying your music collection in your pocket. The device comes in five stunning colors, giving you plenty of options to choose from. Listen to your favorite songs from Apple Music and iTunes. It also offers the ultimate mobile gaming experience for versatility,
        "longDescription": **"<b><br>Apple iPod touch 32GB:</b><br><ul><li>Memory: 32 GB</li><li>Portable iPod touch has touchscreen controls</li><li>Bluetooth capable</li><li>Wireless LAN</li><li>Battery is built-in</li><li>4" retina display</li><li>1080p HD video recording</li><li>Up to 40 hours audio playback</li><li>8-hour video</li><li>8 Megapixel camera</li><li>Includes ear buds and charging cable</li><li>Available in Blue, Gold, Pink, White and Silver and Space Gray</li></ul>**",
}

В ответе JSON, упомянутом выше, я хочу пропустить теги HTML при получении ответа.

1 ответ

Вы можете использовать функцию ниже, чтобы удалить HTML-тегиfunction strip_html_tags(str) { if (!str) return; else str = str.toString(); return str.replace(/<[^>]*>/g, ''); }

Эта простая функция должна декодировать html-объекты, а затем вырезать html-теги из текста, который вы получаете из этого API:

function stripHtmlEntities(str) {
    let decodedHtml = decodeEntities(str);
    let el = document.createElement('span');
    el.innerHTML = decodedHtml;
    return el.textContent || el.innerText;
}
Другие вопросы по тегам