You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON.parse解析失败但$.parseJSON可正常解析、后续JSON.stringify报错的异常问题排查

Troubleshooting the "JSON.parse is not a function" Error with Your Specific JSON String

Let's break down what's happening here—this error almost never means JSON.parse actually isn't a function. It's a clear sign something is messing with the global JSON object in your Chrome environment.

First, Rule Out JSON Syntax Issues

Your JSON string (after replacing " with regular quotes) is completely valid:

[{ "id" : "3", "parent" : "2", "text" : "Lack of Survey Information" },{ "id" : "4", "parent" : "3", "text" : "Lack of Surveyor Resource" },{ "id" : "5", "parent" : "3", "text" : "Land access unavailable" },{ "id" : "2", "parent" : "1", "text" : "Design Incorrect" },{ "id" : "1", "parent" : "16", "text" : "Abortive Work" },{ "id" : "6", "parent" : "1", "text" : "Wrong design info used in construction" },{ "id" : "16", "parent" : "#", "text" : "a" }]

The # character in the parent field is totally allowed in JSON strings, which is why your JSFiddle test worked fine. Syntax isn't the problem here.

Why $.parseJSON Works But JSON.parse Doesn't

jQuery's $.parseJSON uses its own internal parsing logic (it doesn't rely directly on the global JSON object, especially in older jQuery versions). That's why it can bypass whatever's broken with your global JSON setup.

The real issue is that somewhere in your code or a third-party script, the global JSON object is being overwritten or modified. Common scenarios include:

  • You accidentally declared a variable named JSON (e.g., var JSON = "some string"; or const JSON = {};), which replaces the native global JSON object.
  • A third-party script is modifying the JSON object in a way that breaks its parse or stringify methods.

When this happens, JSON is no longer the native object with standard methods—so calling JSON.parse() throws that confusing "not a function" error, and JSON.stringify() crashes for the same reason.

How to Verify and Fix This

  1. Check what JSON actually is in your environment:
    Open Chrome's DevTools console right before the line that calls JSON.parse, and run:

    console.log(JSON);
    console.log(typeof JSON);
    

    If you see a string, custom object, or anything other than the native JSON object (which should show up as JSON { parse: ƒ, stringify: ƒ, ... }), you've found the culprit.

  2. Hunt down the code overriding JSON:

    • Search your codebase for any lines declaring var JSON, let JSON, or const JSON. Rename those variables immediately—never use JSON as a variable name.
    • Check third-party scripts you're loading (like old plugins or analytics tools). If one is modifying the global JSON object, consider updating it to a newer version, replacing it with an alternative, or wrapping your code in an IIFE (Immediately Invoked Function Expression) to isolate your scope from global pollution.
  3. Test in a clean environment:
    Run just the JSON parsing code in a fresh Chrome tab (without any of your project's scripts loaded). It should work fine, confirming the problem is specific to your project's code/script setup.


内容的提问来源于stack exchange,提问作者Andrew Watts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:44:09