javascript - 将 HTML 负载解析为 JSON

标签 javascript node.js json

我遇到以下有效负载的问题。

我在 Node.js 后端通过 API 调用收到的负载如下。

{
  "id": "1",
  "content":{
     "text": "
         <body>
           <div>
             <table>
               <thead>
                <tr>
                    <th><strong>Header 1</strong></th>
                    <th><strong>Header 2</strong></th>
                </tr>
               </thead>
                <tbody>
                 <tr>
                    <td>question 1</td>
                    <td>answer 1</td>
                 </tr>
                 <tr>
                    <td>question 2</td>
                    <td>answer 2</td>
                 </tr>
                 <tr>
                    <td>question 3</td>
                    <td>answer 3</td>
                 </tr>
                </tbody>
             </table>
           </div>
         </body>
         "
   }
}

所以我在这里存储响应如下:

var response = data

即data 是 JSON 响应

我保存 HTML 数据如下

var HTMLContentText = response.content.text

结果我会得到这个:

         <body>
           <div>
             <table>
               <thead>
                <tr>
                    <th><strong>Header 1</strong></th>
                    <th><strong>Header 2</strong></th>
                </tr>
               </thead>
                <tbody>
                 <tr>
                    <td>question 1</td>
                    <td>answer 1</td>
                 </tr>
                 <tr>
                    <td>question 2</td>
                    <td>answer 2</td>
                 </tr>
                 <tr>
                    <td>question 3</td>
                    <td>answer 3</td>
                 </tr>
                </tbody>
             </table>
           </div>
         </body>

这里我要执行以下操作

  1. 将 HTML 文本解析为对象
  2. 从响应中获取表格 即 select("table").first();
  3. 获取表格的行数 即选择(“tr”)

我在 JAVA 中有相同的代码。

这里仅供引用。在这里,我使用 Jsoup 解析器来完成所有操作。我现在想用 Javascript 执行所有操作。

        // HTML as text (from JSON)
        String HtmlFormattedText = (String)((JSONObject)JsonObject.get("content")).get("text");

        // parse the html text into an object
        Document HtmlFormattedDocumentObject = Jsoup.parse(HtmlFormattedText);

        // get the table from the response
        Element allRowsFromTable = HtmlFormattedDocumentObject.select("table").first();

        // get the rows of the table
        return allRowsFromTable.select("tr");

最佳答案

我为此创建了一个片段。函数的返回值包含表的所有 tr 元素 - 您无需先选择表。

const response = {
  "id": "1",
  "content": {
    "text": `
         <body>
           <div>
             <table>
               <thead>
                <tr>
                    <th><strong>Header 1</strong></th>
                    <th><strong>Header 2</strong></th>
                </tr>
               </thead>
                <tbody>
                 <tr>
                    <td>question 1</td>
                    <td>answer 1</td>
                 </tr>
                 <tr>
                    <td>question 2</td>
                    <td>answer 2</td>
                 </tr>
                 <tr>
                    <td>question 3</td>
                    <td>answer 3</td>
                 </tr>
                </tbody>
             </table>
           </div>
         </body>
         `
  }
}

// logging the result of the function
console.log(parseTable(response.content.text))

function parseTable(string) {
  // creating an HTML element
  const el = document.createElement('html')

  // adding the string to the HTML element
  el.innerHTML = string

  // selecting and returning all tr elements
  return el.getElementsByTagName('tr')
}

注意:我将数据中的单引号更改为反引号,因为它允许多行字符串。

关于javascript - 将 HTML 负载解析为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57611439/

相关文章:

javascript - Node - 无数据创建的 ES 索引

php - 从 Ajax 格式化 jQuery 数据表的 PHP/Mysql 数据

javascript - 我怎样才能让我的脚本工作/响应

javascript - 使用按钮和 JS 水平滚动隐藏的 overflow-x

javascript - JSON 总是以 [null

javascript - 在第一个 Jquery 完成之前第二个 Jquery json 被触发

c# - 如何使用 JSON.net 格式化嵌套 JSON 对象的输出?

c# - 服务器端验证完成后运行 javascript 函数

javascript - Internet Explorer Javascript 的 AppendChild 问题

node.js - Node js + socket io 仍然在客户端显示错误,如何修复? CentOS7