gpt4 book ai didi

javascript - 从 JSON 中删除 src 以创建图像

转载 作者:行者123 更新时间:2023-11-30 19:04:34 25 4
gpt4 key购买 nike

我知道这可能以前做过,但我找不到合适的关键字在谷歌上搜索这个。基本上我想要做的是创建一个图像元素以响应响应对象。但是响应对象包含 html 标签,所以我不能直接创建图像。任何人都可以帮助我实现这一目标。这是我要从中显示图像的对象属性

{
"content": "<img src='https://i.cbc.ca/1.5379524.1575076619!/fileImage/httpImage/image.JPG_gen/derivatives/16x9_460/czech-china.JPG' alt='CZECH-CHINA/' width='460' title='Czech Republic&#39;s President Milos Zeman (R) and his Chinese counterpart Xi Jinping walk past an honour guard at Prague Castle in Prague, Czech Republic, March 29, 2016. ' height='259' /> <p>A half-decade ago, China planted an economic flag in Central Europe with its multi-billion-dollar Belt and Road plan to build a vast infrastructure network. Five years on, though, some of those countries are finding the relationship increasingly uncomfortable.</p>"
}

我如何破译此内容以从此处获取文本和图像。正如@Renaldo Balaj 所建议的,我尝试使用 html 来响应包,但我仍然得到一个未在图像或段落中呈现的字符串。这是我的 JSX

<div className="dark-overlay">
<div className="news">
<Carousel>
{ news.items.map( news => {
const htmlInput = news.content;
const htmlToReactParser = new HtmlToReactParser();
const reactElement = htmlToReactParser.parse(htmlInput);
const reactHtml = ReactDOMServer.renderToStaticMarkup(reactElement);
console.log(reactHtml);
return <Carousel.Item key={news.guid}>
{reactHtml}

</Carousel.Item>;
}
)}
</Carousel>
</div>
</div>

最佳答案

不需要获取src,直接从后台获取img即可打印

为此构建的软件包,例如:react-html-parser (您只需要输入“<img >”,它就会返回一个完全可用的 img 标签)

我在评论中看到您在实现它时遇到了问题,这是工作演示: https://stackblitz.com/edit/react-ncvxgb?file=index.js

关于javascript - 从 JSON 中删除 src 以创建图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59122014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com