3 回答
TA贡献1890条经验 获得超9个赞
这是 JSON.parse 的一个很好的候选者;但是,单引号应该转换为双引号才能起作用。小心转义引号,您可以尝试:
const raw = `[[24297, 'CWCS Sump 002', -121.50842, 38.54798, '2', 'SEWER SUMP'],[8035, 'CWCS Sump 002A (56)', -121.50842, 38.54798, '2A', 'SEWER SUMP'],[9334, 'CWCS Sump 001', -121.5110297, 38.5703431, '1', 'SEWER SUMP']]`;
const res = JSON.parse(raw.replace(/(?<!\\)'/g, `"`));
console.log(res);
TA贡献1839条经验 获得超15个赞
你可以用JSON.parse来做:
let str = '[[24297, "CWCS Sump 002", -121.50842, 38.54798, "2", "SEWER SUMP"],[8035, "CWCS Sump 002A (56)", -121.50842, 38.54798, "2A", "SEWER SUMP"]]';
let arr = JSON.parse(str);
请注意,最好'将整个字符串的"周围环绕在内部的字符串周围。可以这样做:
str.replace("'", """);
TA贡献1824条经验 获得超5个赞
据我说这不会是直接的。你可以这样试试。
let a = "[
[24297,'CWCS Sump 002',-121.50842, 38.54798,'2','SEWER SUMP'],
[8035,'CWCS Sump 002A (56)',-121.50842,38.54798,'2A','SEWER SUMP'],
[9334,'CWCS Sump 001', -121.5110297,38.5703431,'1','SEWER SUMP']
]";
let c= [];
a.replace('[[','').replace(']]','').split('],[').forEach(
(x)=>{c.push(x.split(','))}
);
//c is the result
添加回答
举报