Javascript 将数据json从web保存到json文件

Javascript 将数据json从web保存到json文件,javascript,Javascript,所以我有这样的代码清理。但当我保存到json文件时,这段代码会出错 按摩eror=TypeError:将循环结构转换为JSON 从构造函数为“Node”的对象开始 属性'children'->构造函数为'Array'的对象 索引0->具有构造函数“节点”的对象 属性“parent”关闭该圆 在JSON.stringify()上 const axios=require('axios'); const cheerio=需要(“cheerio”); var fs=需要('fs'); 常量url=htt

所以我有这样的代码清理。但当我保存到json文件时,这段代码会出错

按摩eror=TypeError:将循环结构转换为JSON 从构造函数为“Node”的对象开始 属性'children'->构造函数为'Array'的对象 索引0->具有构造函数“节点”的对象 属性“parent”关闭该圆 在JSON.stringify()上

const axios=require('axios');
const cheerio=需要(“cheerio”);
var fs=需要('fs');
常量url=https://icons8.com/line-awesome';
axios(url)
。然后(响应=>{
const html=response.data;
const$=cheerio.load(html)
常量列表图标=$('.icons组');
常量列表=[];
列表图标。每个(函数(){
const title=$(this.find('.icons title').text();
常量图标=[];
const panjang=$(this.find('.name');
for(设i=0;i
每个HTML节点都在引用其他HTML节点的某个位置。就像一个节点引用其父节点一样,一个节点的父节点引用其父节点的父节点。这使得它是循环的,所以这就是你的原因。只需使用像
title
innerText
这样的需要字段,而不是获取所有节点对象。

这是否回答了您的问题?请看。它提出了避免JSON中循环结构的方法。
const axios = require('axios');
const cheerio = require('cheerio');
var fs = require('fs');
const url = 'https://icons8.com/line-awesome';

axios(url)
 .then(response => {
   const html = response.data;
   const $ = cheerio.load(html)
   const listIcons = $('.icons-group');
   const list = [];

listIcons.each(function () {
  const title = $(this).find('.icons-title').text();
  const icons = [];
  const panjang = $(this).find('.name');
  for (let i = 0; i < panjang.length; i++) {
    icons.push($(panjang[i]).text());
  }
//   console.log(panjang.length);
//   console.log(panjang[0]);
  list.push({
    title,
    icons,
  });
  console.log(list);
 });
  fs.writeFile('nama.json', JSON.stringify(listIcons, null, 4));
})
.catch(console.error);