我正在執行網路抓取任務,我想從該資料創建一個 JSON 物件。這就是我試圖做的來生成我的 JSON。
var submitButton = driver.findElement(By.className('btn btn-primary'));
submitButton.click().then(function () {
setTimeout(async function () {
const pagesource = await driver.getPageSource();
const $ = cheerio.load(pagesource);
const tableCount = $('.table , .table-bordered').length;
const tablesJsonArray = [];
for (let i = 0; i < tableCount; i ) {
const subjectsJsonArray = [];
const tableData = $('.table , .table-bordered').eq(i); // HTML table (Academic Year 1/2/3)
const subjectCount = tableData.children('tbody').children('tr').length;
for (let j = 0; j < subjectCount; j ) {
const subjectData = tableData.children('tbody').children('tr').eq(j); // table row
const subjectName = subjectData.children('td').eq(0).text();
const year = subjectData.children('td').eq(1).text();
const credits = subjectData.children('td').eq(2).text();
const sOrder = subjectData.children('td').eq(3).text();
const result = subjectData.children('td').eq(4).text();
const onlineAssignmentResult = subjectData.children('td').eq(5).text();
const subjectDataObj = {
subject_name: subjectName.trim(),
year: year,
credits: credits,
s_order: sOrder,
result: result,
online_assignment_result: onlineAssignmentResult.trim(),
};
const subjectJsonString = JSON.stringify(subjectDataObj);
const subjectJSON = JSON.parse(subjectJsonString);
subjectsJsonArray.push(subjectJSON);
}
const resultObj = {
table: i,
data: subjectsJsonArray
};
const resultJSON = JSON.parse(JSON.stringify(resultObj));
tablesJsonArray.push(resultJSON);
}
console.log(tablesJsonArray);
}, 3000);
});
當我運行此代碼時,控制臺輸出如下,
[
{
table: 0,
data: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
]
},
{
table: 1,
data: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
]
},
{
table: 2,
data: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
]
}
]
“resultObj”物件中的“subjectsJsonArray”不會轉換為 JSON,僅顯示為 [Object]。從“resultObj”(具有嵌套物件)創建有效 JSON 的正確方法是什么?
下面是我需要的有效結果 JSON(在這個例子中只有 3 個物件顯示在 'data' 中),
[
{
"table": "0",
"data": [
{
"subject_name": "IT1105 Information Systems & Technology",
"year": "[2017]",
"credits": "3",
"s_order": "[1]",
"result": "B-",
"online_assignment_result": "P"
},
{
"subject_name": "IT1205 Computer Systems I",
"year": "[2017]",
"credits": "3",
"s_order": "[2]",
"result": "C",
"online_assignment_result": "P"
},
{
"subject_name": "IT1305 Web Application Development I",
"year": "[2017]",
"credits": "3",
"s_order": "[3]",
"result": "B-",
"online_assignment_result": "P"
}
]
},
{
"table": "1",
"data": [
{
"subject_name": "IT3105 Object Oriented Analysis & Design",
"year": "[2018]",
"credits": "3",
"s_order": "[1]",
"result": "C ",
"online_assignment_result": "P"
},
{
"subject_name": "IT3205 Fundamentals of Software Engineering",
"year": "[2018]",
"credits": "3",
"s_order": "[2]",
"result": "A-",
"online_assignment_result": "P"
},
{
"subject_name": "IT3305 Mathematics for Computing II",
"year": "[2018]",
"credits": "3",
"s_order": "[3]",
"result": "C",
"online_assignment_result": "P"
}
]
},
{
"table": "2",
"data": [
{
"subject_name": "IT5105 Professional Issues in IT",
"year": "[2019]",
"credits": "3",
"s_order": "[0]",
"result": "B",
"online_assignment_result": "-"
},
{
"subject_name": "IT5405 Fundamentals of Multimedia",
"year": "[2019]",
"credits": "3",
"s_order": "[0]",
"result": "B ",
"online_assignment_result": "-"
},
{
"subject_name": "IT6205 Systems & Network Administration",
"year": "[2019]",
"credits": "3",
"s_order": "[0]",
"result": "C",
"online_assignment_result": "-"
}
]
}
]
感謝您作為新手對此的幫助。謝謝!
uj5u.com熱心網友回復:
只需洗掉這兩行:
const subjectJsonString = JSON.stringify(subjectDataObj);
const subjectJSON = JSON.parse(subjectJsonString);
并編輯這一行:從subjectsJsonArray.push(subjectJSON);
到subjectsJsonArray.push(subjectDataObj);
uj5u.com熱心網友回復:
你可以試試這個代碼。這里 'obj' 是您的嵌套物件。
const newObj = JSON.stringify(obj, " ", 2);
console.log(newObj);
在這里,您可以通過更改 stringify() 中的第三個引數來更改空間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317050.html
標籤:javascript 节点.js json 网页抓取 网络部署
