Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript节点中处理容易出错的JSON?_Javascript_Json - Fatal编程技术网

如何在JavaScript节点中处理容易出错的JSON?

如何在JavaScript节点中处理容易出错的JSON?,javascript,json,Javascript,Json,我正在向一个API发出请求,大多数时候我收到的响应都是有效的JSON,但是,偶尔JSON会有点破损,JavaScript无法处理 以该请求为例: 当您将JSON粘贴到此网站时:网站上的JSON格式化程序意识到JSON已损坏,但仍能很好地修复它 有没有办法在JavaScript中实现这一点 下面是我的一段代码,它在这里中断: async function scraper(url) { const browser = await puppeteer.launch({headless: fal

我正在向一个API发出请求,大多数时候我收到的响应都是有效的JSON,但是,偶尔JSON会有点破损,JavaScript无法处理

以该请求为例:

当您将JSON粘贴到此网站时:网站上的JSON格式化程序意识到JSON已损坏,但仍能很好地修复它

有没有办法在JavaScript中实现这一点

下面是我的一段代码,它在这里中断:

async function scraper(url) {
    const browser = await puppeteer.launch({headless: false})
    const page = await browser.newPage();
    await page.waitFor(2000)
    await page.goto(url);
    const content = await page.content();

    if (initialFlag) {
        innerText = await page.evaluate(() => {
            let wholeJson = JSON.parse(document.querySelector("body").innerText);
            let status = wholeJson[0].status;
            if (status === "REMOVED") {
                return {status: status}
            }
它在
innerText=wait page.evaluate(()=>{
行上中断

完整的错误消息是:

Error: Evaluation failed: SyntaxError: Unexpected token 
 in JSON at position 1831
    at JSON.parse (<anonymous>)
    at __puppeteer_evaluation_script__:2:34
    at ExecutionContext._evaluateInternal (C:\Users\user\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:217:19)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async ExecutionContext.evaluate (C:\Users\user\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:106:16)
    at async scraper (C:\Users\user\Documents\Node Projects\bergi.js:23:21)
    at async main (C:\Users\user\Documents\Node Projects\bergi.js:77:27)

错误:评估失败:语法错误:意外标记
在位置1831处的JSON中
在JSON.parse()处
在(木偶演员)(评估)(剧本):2:34
在ExecutionContext.\u evaluateInternal(C:\Users\user\node\u modules\puppeter\lib\cjs\puppeter\common\ExecutionContext.js:217:19)
在处理和拒绝时(内部/process/task_queues.js:97:5)
在async ExecutionContext.evaluate(C:\Users\user\node\u modules\puppeter\lib\cjs\puppeter\common\ExecutionContext.js:106:16)
异步刮板(C:\Users\user\Documents\Node Projects\bergi.js:23:21)
在async main(C:\Users\user\Documents\Node Projects\bergi.js:77:27)