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

使用JSON.parse解析字符串数组失败,咨询更优替代解析方案

解决JSON.parse单引号报错的替代解析方法

Hey there! Let's clear up why you're seeing that error first, then dive into the practical alternative methods you're looking for.

Why the Error Happens

The root issue boils down to a key syntax difference between JavaScript literals and strict JSON:

  • JavaScript lets you use single quotes (') to wrap string values in arrays or objects—this is why your direct array declaration works perfectly.
  • JSON has strict rules: all strings (and object keys) must be wrapped in double quotes ("). Single quotes are invalid JSON syntax, which is exactly why JSON.parse throws that SyntaxError.

Alternative Parsing Methods (No Custom Parser Needed)

Here are reliable options to handle this without building your own parser:

1. Sanitize the String for JSON.parse

If your input only uses single quotes (and no escaped single quotes like \'), you can safely convert them to double quotes before parsing:

const inputStr = "['Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness']";
const validJsonStr = inputStr.replace(/'/g, '"');
const arr = JSON.parse(validJsonStr);

Important note: If your string contains escaped single quotes (e.g., ['It\'s a critical test']), the simple replace above will break things. For those cases, use a smarter replacement that skips escaped quotes:

const validJsonStr = inputStr.replace(/(?<!\\)'/g, '"').replace(/\\'/g, "'");

This replaces only unescaped single quotes with double quotes, then converts escaped single quotes back to regular single quotes inside the string content.

2. Use eval() (Only for Trusted Content!)

Since your input string is already valid JavaScript syntax, eval() can directly parse it into an array. This is super quick, but never use this if the input comes from an untrusted source (like user input)—eval() executes any code it receives, which is a huge security risk.

const inputStr = "['Testing avionics, electrical and mission wiring, verifying the correct installation of cabling, solving harness']";
const arr = eval(inputStr);

Stick to this only when you're 100% sure the input is safe and fully under your control.

3. Use a Lenient JSON Parser Library

There are dedicated libraries built to parse "relaxed" JSON syntax (including single quotes, unquoted object keys, trailing commas, etc.). These tools handle cases where strict JSON fails, without requiring you to write custom parsing logic. Look for packages that support "JSON5" or "loose JSON" syntax—they'll process your input seamlessly.

Final Recommendation

  • For trusted, simple inputs: eval() is the fastest, most straightforward choice.
  • For untrusted inputs: Use the string sanitization method (with proper escape handling) or a lenient JSON library to avoid security vulnerabilities.

内容的提问来源于stack exchange,提问作者François MENTEC

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:06:52