【问题标题】:convert complex javascript object to JSON nodejs将复杂的 javascript 对象转换为 JSON nodejs
【发布时间】:2021-10-11 07:26:18
【问题描述】:

我正在执行网络抓取任务,我想从该数据创建一个 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": "-"
            }
        ]
    }
]

感谢您作为新手对此提供的帮助。谢谢!

【问题讨论】:

  • 你为什么要跳那些JSON.parse(JSON.stringify())的舞蹈?只需将原始对象推入您的列表并在最后执行 JSON。
  • 其实我是新手,你是不是这个意思const resultJSON = JSON.stringify(resultObj));?还是没有字符串化?
  • 您不需要使用任何 JSON 函数,直到您将数据实际输出到例如一个文件。

标签: javascript node.js json web-scraping web-deployment


【解决方案1】:

只需删除这两行:

const subjectJsonString = JSON.stringify(subjectDataObj);

const subjectJSON = JSON.parse(subjectJsonString);

并编辑这一行: 来自subjectsJsonArray.push(subjectJSON);subjectsJsonArray.push(subjectDataObj);

【讨论】:

    【解决方案2】:

    你可以试试这个代码。这里的 'obj' 是你的嵌套对象。

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

    这里你可以通过改变stringify()中的第三个参数来改变空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多