微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从JSON字符串中提取图像

如何解决从JSON字符串中提取图像

任务:将其视为字符串,并将字符串中存在的所有图像复制到您选择的任何数据库中,并使用新的URL重建字符串

db2move

解决方法

也许你是这个意思?

PS:由于\ n和嵌套MathML中的双引号,您的JSON(如果应该如此)无效,

let string = `{"_items": [{"_id": "5f5b6acab4175c979b8cb5af","owning_partner_id": 10304,"type": "Question","subtype": "Subjective","modifier": "Judgemental","status": "Draft","author_id": 4542,"assigned_to": 4542,"content": {"question_details": {"en": {"answers": [{"body": "","is_correct": true,"explanation": ".","code": "a"}],"question_txt": "

In the figure,a rectangular block is gradually pushed face down into a liquid. The block has height <math xmlns="http://www.w3.org/1998/Math/MathML">d; on the bottom and top the face area is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">A=8.00 cm2. In the figure,gives the apparent weight <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">Wapp of the block as a function of the depth <math xmlns="http://www.w3.org/1998/Math/MathML">h of its lower face. The scale on the vertical axis is set by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">W<mi mathvariant="normal">s=0.20 <mi mathvariant="normal">N. What is the density of the liquid ?

\n\n
<img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.lusIzGK6zcFw1rxBK38KHQHaE8%26pid%3DApi&f=1" style="width: 245px; height: 150px;" />  <img src="https://static.theceomagazine.net/wp-content/uploads/2018/10/15093202/elon-musk-1100x733.jpg" style="width: 245px; height: 197px;" /> 

\n"}},"book_toc": [],"question_meta_tags": [{"difficulty_level": "","primary_concept": "","ideal_time": "","secondary_concept": [],"learning_maps": [],"learning_maps_data": []}]},"superseded_by": null,"language": "en","is_atg_ready": 0,"created_by": 4542,"updated_by": 4542,"is_prof_approved": 0,"content_schema_version": 1,"version": 1,"_updated": "Fri,11 Sep 2020 12:17:14 GMT","_created": "Fri,"_version": 1,"tenant_id": 1,"title": "
In the figure,gives the apparent weight <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">Wapp of the block as a function of the depth <math xmlns="http://www.w3.org/1998/Math/MathML">h of its lower face. The scale on the vertical axis is set by <math xmlns="http://www.w3.org/1998/Math/MathML"><mi mathvariant="normal">W<mi mathvariant="normal">s=0.20 <mi mathvariant="normal">N. What is the density of the liquid ?

\n\n
<img src="https://www.teslarati.com/wp-content/uploads/2018/08/elon-musk-1-AP.jpg" style="width: 245px; height: 150px;" />  <img src="https://www.teslarati.com/wp-content/uploads/2018/08/elon-musk-1-AP.jpg" style="width: 245px; height: 197px;" /> 

\n","id": 6126381,"question_code": "EM6126381-en","uuid": "d00dfd63-082e-4062-8f1f-e37e7c9f0829","created_at": "Fri,"updated_at": "Fri,"_etag": "3027df270a05e67d65b254d9b134bab952cbcd68","_links": {"self": {"title": "learning_objects","href": "learning_objects/5f5b6acab4175c979b8cb5af"}},"_latest_version": 1,"user_name": "reshma awale"}],"_links": {"parent": {"title": "home","href": "/"},"self": {"title": "learning_objects","href": "learning_objects?where={"type":"Question","$or":[{"id":{"$in":[6126381]}},{"question_code":{"$in":[]}}]}&sort=id&embed=true"}},"_meta": {"page": 1,"max_results": 25,"total": 1}}`

const images = string.match(/<img[^>]+src="([^">]+)"/g)
const div = document.createElement("div");
div.innerHTML += images.join("<br/>"); // you can use a div in memory

const sources = [...div.querySelectorAll("img")].map(img => img.src)
console.log(sources)
sources.forEach(src => {
  const url = new URL(src)
  url.hostname = "yourhost.com"; 
  string = string.split(src).join(url.href);
})
console.log(string)

将上面的forEach替换为未经测试的代码

let counter = sources.length
var saveUrlAndRetrieveNewUrl = async() => {
  while (true) {
    var res = await fetch(`/yourdbstore.php?url=${sources[--counter]}`); // the php will get the image,store it and return new URL
    if (counter = 0) break; // stop
    const data = await res.text();
    const url = new URL(data)
    url.hostname = "yourhost.com";
    string = string.split(src).join(url.href);
  };
  console.log("done");
};
saveUrlAndRetrieveNewUrl();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。