javascript - file_get_contents 到 json 数组
问题描述
我有使用以下代码通过 PHP 读取的 HTML 文件:
$d = new DOMDocument;
$mock = new DOMDocument;
$d->loadHTML(file_get_contents('hydrocarbon2.htm'));
$body = $d->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child){
$mock->appendChild($mock->importNode($child, true));
}
//echo $mock->saveHTML();
$htmlcode = $mock->saveHTML();
echo $htmlcode;
我创建了一个代码来将 html 代码分解为下面的 json::
var myRows = [];
var $headers = $("th");
var $rows = $("tbody tr").each(function(index) {
$cells = $(this).find("td");
myRows[index] = {};
$cells.each(function(cellIndex) {
myRows[index][$($headers[cellIndex]).html()] = $(this).html();
});
});
var myObj = {};
myObj.myrows = myRows;
alert(JSON.stringify(myObj));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>
</thead>
<tbody>
<tr> <td>Q</td><td>Desc.</td> </tr>
<tr> <td>Type</td><td>Multiple choice</td> </tr>
<tr><td>Option</td><td>image #####2</td><td>incorrect</td></tr>
<tr><td>Option</td><td>image #####2</td><td>incorrect</td></tr>
<tr><td>Option</td><td>image #####2</td><td>incorrect</td></tr>
<tr><td>Option</td><td>image #####2</td><td>incorrect</td></tr>
<tr><td>Solution</td><td>Some text / image</td></tr>
<tr><td>Marks</td><td>4</td><td>1</td></tr>
</tbody>
</table>
我需要转换$htmlcode
成 JSON 数组。我如何合并这两者?
解决方案
推荐阅读
- python - change value of boolean field in admin pannel in Django
- python - 有没有一种简单的方法可以在 python 中接收 Http POST 请求,比如在 php 中?
- java - org.json.JSONException: No value for senses
- python - How to divide a text file sectionwise using Python
- .net - Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzer 引发了“System.NullReferenceException”类型的异常
- android - Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference issue
- java - Can some one get jar from a docker image and reverse engineer the jar to get the source code?
- javascript - How can I implement authentication with next-iron-session using getServerSideProps without including the same code on each page
- python - Replace all Non-Alphanumeric Characters except one particular pattern using RegEx in Python
- powerbi - 根据一个月计算工作日数