如何收集和解析HTML中的JSON数据

问题描述 投票:0回答:1

我最终希望创建一个HTML文件,该文件将完成以下任务。关键是,我需要将其包含在HTML代码段/文件中。

  1. 对后端CMS(即Drupal)进行GET api调用。此api调用返回特定内容项的以下JSON数据:
{
    "jsonapi": {
        "version": "1.0",
        "meta": {
            "links": {
                "self": {
                    "href": "http://jsonapi.org/format/1.0/"
                }
            }
        }
    },
    "data": {
        "type": "node--espot_html",
        "id": "fbaab7dd-09c2-4aa0-852d-4b9462074a45",
        "attributes": {
            "drupal_internal__nid": 1,
            "drupal_internal__vid": 11,
            "langcode": "en",
            "revision_timestamp": "2019-10-31T13:29:29+00:00",
            "revision_log": null,
            "status": true,
            "title": "Razor Hero Image",
            "created": "2019-10-28T16:24:20+00:00",
            "changed": "2019-10-31T13:29:29+00:00",
            "promote": true,
            "sticky": false,
            "default_langcode": true,
            "revision_translation_affected": true,
            "path": {
                "alias": "/razor-hero",
                "pid": 1,
                "langcode": "en"
            },
            "body": {
                "value": "<div class=\"jns-hero-image jns-hero-image-message-left\" id=\"hero_slideshow\"><img alt=\"Clean Shave\" data-entity-type=\"file\" data-entity-uuid=\"fbc875d0-ef17-4813-981c-22a29e42c44f\" src=\"/sites/default/files/inline-images/dont-shave-2.jpg\" />\r\n<div class=\"grid-container\">\r\n<div class=\"grid-x grid-padding-x\">\r\n<div class=\"small-12 medium-8 cell hero-message\">\r\n<div>\r\n<h1 class=\"text-hero text-hero-left\">Get a Much Better<br />\r\nShave</h1>\r\n\r\n<p>Fewer things look and feel better than a clean shave!</p>\r\n<a>SHOP NOW</a></div>\r\n</div>\r\n</div>\r\n</div>\r\n</div>\r\n",
                "format": "full_html",
                "processed": "<div class=\"jns-hero-image jns-hero-image-message-left\" id=\"hero_slideshow\"><img alt=\"Clean Shave\" data-entity-type=\"file\" data-entity-uuid=\"fbc875d0-ef17-4813-981c-22a29e42c44f\" src=\"/sites/default/files/inline-images/dont-shave-2.jpg\" /><div class=\"grid-container\">\n<div class=\"grid-x grid-padding-x\">\n<div class=\"small-12 medium-8 cell hero-message\">\n<div>\n<h1 class=\"text-hero text-hero-left\">Get a Much Better<br />\nShave</h1>\n\n<p>Fewer things look and feel better than a clean shave!</p>\n<a>SHOP NOW</a></div>\n</div>\n</div>\n</div>\n</div>\n",
                "summary": ""
            }
        },
        "relationships": {
            "node_type": {
                "data": {
                    "type": "node_type--node_type",
                    "id": "837a5cbe-f8fe-4c03-a613-2092dff1168e"
                },
                "links": {
                    "self": {
                        "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45/relationships/node_type?resourceVersion=id%3A11"
                    },
                    "related": {
                        "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45/node_type?resourceVersion=id%3A11"
                    }
                }
            },
            "revision_uid": {
                "data": {
                    "type": "user--user",
                    "id": "c0d80edb-325a-4ad7-9be3-bc9dc32ed878"
                },
                "links": {
                    "self": {
                        "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45/relationships/revision_uid?resourceVersion=id%3A11"
                    },
                    "related": {
                        "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45/revision_uid?resourceVersion=id%3A11"
                    }
                }
            },
            "uid": {
                "data": {
                    "type": "user--user",
                    "id": "c0d80edb-325a-4ad7-9be3-bc9dc32ed878"
                },
                "links": {
                    "self": {
                        "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45/relationships/uid?resourceVersion=id%3A11"
                    },
                    "related": {
                        "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45/uid?resourceVersion=id%3A11"
                    }
                }
            }
        },
        "links": {
            "self": {
                "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45?resourceVersion=id%3A11"
            }
        }
    },
    "links": {
        "self": {
            "href": "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45"
        }
    }
}
  1. 我正在寻找要解析的JSON响应,以仅提取“值”:对象下的数据。

  2. 我需要删除转义字符

  3. 最终结果应该是在浏览器中呈现了HTML(如下所示):
<div class="jns-hero-image jns-hero-image-message-left" id="hero_slideshow"><img alt="Clean Shave" data-entity-type="file" data-entity-uuid="fbc875d0-ef17-4813-981c-22a29e42c44f" src="/sites/default/files/inline-images/dont-shave-2.jpg" /><div class="grid-container"><div class="grid-x grid-padding-x"><div class="small-12 medium-8 cell hero-message"><div><h1 class="text-hero text-hero-left">Get a Much Better<br />Shave</h1><p>Fewer things look and feel better than a clean shave!</p><a>SHOP NOW</a></div></div></div></div></div>"
  1. 如果成功处理,则应在网页上显示:Screenshot if image rendered via HTML

  2. 这是我尝试过的HTML。我为违反礼节而道歉,我不知道这会被视为如此。在我的浏览器控制台中,它告诉我forEach不是有效的函数或此响应。

'<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Ghibli App</title>

    <link href="https://fonts.googleapis.com/css?family=Dosis:400,700" rel="stylesheet" />
    <!-- <link href="style.css" rel="stylesheet" /> -->
  </head>
 

  <body>
    <div id="root"></div>
     <script>
	 const app = document.getElementById('root');

const container = document.createElement('div');
container.setAttribute('class', 'container');

app.appendChild(logo);
app.appendChild(container);

var request = new XMLHttpRequest();
request.open('GET', 'http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45', true);
request.onload = function () {

 // Begin accessing JSON data here
 var data = JSON.parse(this.response);
 if (request.status >= 200 && request.status < 400) {
   data.forEach(value => {
     const card = document.createElement('div');
     card.setAttribute('class', 'card');

     const h1 = document.createElement('h1');
     h1.textContent = valuable.title;

     const p = document.createElement('p');
     value.description = value.description.substring(0, 300);
     p.textContent = `${value.description}...`;

     container.appendChild(card);
     card.appendChild(h1);
     card.appendChild(p);
   });
 } else {
   const errorMessage = document.createElement('marquee');
   errorMessage.textContent = `Gah, it's not working!`;
   app.appendChild(errorMessage);
 }
}

request.send();
	 </script>
  </body>
</html>'
javascript html json parsing drupal-8
1个回答
0
投票

这最终产生了我想要产生的最终结果:

<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
<script>
const app = document.getElementById('root');

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
  
    let obj = JSON.parse(this.responseText);
	console.log(obj.data.attributes.body.value)
	app.innerHTML = obj.data.attributes.body.value.replace('src="/sites','src="http://localhost:4700/sites'); 
  }
};
xmlhttp.open("GET", "http://localhost:4700/jsonapi/node/espot_html/fbaab7dd-09c2-4aa0-852d-4b9462074a45", true);
xmlhttp.send();

</script>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.