简体   繁体   English

将复杂的 javascript 对象转换为 JSON nodejs

[英]convert complex javascript object to JSON nodejs

I am doing a web scraping task & I want to create a JSON object from that data.我正在执行网络抓取任务,我想从该数据创建一个 JSON 对象。 This is what I attempt to do to generate my 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); });

When I run this code the console output as below,当我运行此代码时,控制台输出如下,

 [ { 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] ] } ]

The 'subjectsJsonArray' in 'resultObj' object does not convert to JSON & only shows as [Object]. “resultObj”对象中的“subjectsJsonArray”不会转换为 JSON,仅显示为 [Object]。 What is the proper way to create a valid JSON from 'resultObj'(with having a nested object)?从“resultObj”(具有嵌套对象)创建有效 JSON 的正确方法是什么?

Below is the valid result JSON I need (for this example only 3 object shows inside 'data' ),下面是我需要的有效结果 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": "-" } ] } ]

Appreciate your help with this as a newbie.感谢您作为新手对此的帮助。 thanks!谢谢!

Just remove these two lines:只需删除这两行:

const subjectJsonString = JSON.stringify(subjectDataObj);

const subjectJSON = JSON.parse(subjectJsonString);

And edit this line: From subjectsJsonArray.push(subjectJSON);并编辑这一行: From subjectsJsonArray.push(subjectJSON); To subjectsJsonArray.push(subjectDataObj);subjectsJsonArray.push(subjectDataObj);

You can try this code.你可以试试这个代码。 Here 'obj' is your nested object.这里 'obj' 是您的嵌套对象。

const newObj = JSON.stringify(obj, " ", 2);
console.log(newObj);

Here you can change space by changing third parameter in stringify().在这里,您可以通过更改 stringify() 中的第三个参数来更改空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM