【问题标题】:Extract images from JSON string从 JSON 字符串中提取图像
【发布时间】:2021-01-01 01:29:06
【问题描述】:

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

{"_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, 11 Sep 2020 12:17:14 GMT", "_version": 1, "tenant_id": 1, "title": "
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://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, 11 Sep 2020 12:17:14 GMT", "updated_at": "Fri, 11 Sep 2020 12:17:14 GMT", "_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}}

【问题讨论】:

  • 欢迎来到 Stack Overflow!请访问help center,使用tour 了解内容和How to Ask。做一些研究,搜索关于SO的相关主题;如果您遇到困难,请发布您尝试的minimal reproducible example,并使用[&lt;&gt;] sn-p 编辑器记录输入和预期输出。
  • 你的问题到底是什么?
  • 我试图为你提出一个适当的问题。请添加相关信息和您的尝试!
  • 顺便说一句,您的字符串不是有效的 JSON

标签: javascript mathml


【解决方案1】:

也许你是这个意思?

PS:由于嵌套的 MathML 中的 \n 和双引号,您的 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, 11 Sep 2020 12:17:14 GMT", "_version": 1, "tenant_id": 1, "title": "
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://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, 11 Sep 2020 12:17:14 GMT", "updated_at": "Fri, 11 Sep 2020 12:17:14 GMT", "_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();

【讨论】:

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