在 Javascript 中将 \" 替换为 "
Replace \" with " in Javascript
我有一个来自我的 Web 服务器的 JSON,它是从 XMLHttpRequest 获得的。
当我使用
打印出来时,数据具有以下形式
var data = this.responseText;
console.log("data=" + JSON.stringify(data));
data=[{\"day\":0,\"periods\":[\"0xffffffffffff\"]}]
我使用 jquery 处理 JSON 但出现错误:
未捕获的类型错误:无法使用 'in' 运算符在 [{"day":0,"periods":["0xffffffffffff"]}]
中搜索 'length'
我假设问题是由于引号转义造成的,就好像我将数据硬编码为 [{"day":0,"periods":["0xffffffffffff"]}]
我没有收到错误消息。
我已经尝试了多种摆脱转义的方法但没有成功:
data = data.replace(/\/g, "");
根本不修改字符串;
我从另一个线程 replaceAll 找到了一个函数,但是这个:
var newData = data.replaceAll("\","");
..也没有区别。
尝试将 \" 替换为 ' 然后将 ' 替换为 " 只是 returns 我到 \"
var newData = data.replaceAll("\"","'");
now newData = [{'day':0,'periods':['0xffffffffffff']}]
newData = newData.replaceAll("'","\"");
and it's back to [{\"day\":0,\"periods\":[\"0xffffffffffff\"]}]
Trying to process with single quote, i.e. [{'day':0,'periods':['0xffffffffffff']}] gives me the same Uncaught TypeError message.
有什么办法可以解决这个问题吗?
谢谢。
错误是您在字符串上使用了 JSON.stringify
。仔细查看 this.responseText
- responseText - 你正在使用的 属性。只需将其更改为
var data = JSON.parse(this.responseText);
console.log("data=" + data);
我有一个来自我的 Web 服务器的 JSON,它是从 XMLHttpRequest 获得的。 当我使用
打印出来时,数据具有以下形式var data = this.responseText;
console.log("data=" + JSON.stringify(data));
data=[{\"day\":0,\"periods\":[\"0xffffffffffff\"]}]
我使用 jquery 处理 JSON 但出现错误:
未捕获的类型错误:无法使用 'in' 运算符在 [{"day":0,"periods":["0xffffffffffff"]}]
我假设问题是由于引号转义造成的,就好像我将数据硬编码为 [{"day":0,"periods":["0xffffffffffff"]}]
我没有收到错误消息。
我已经尝试了多种摆脱转义的方法但没有成功:
data = data.replace(/\/g, "");
根本不修改字符串; 我从另一个线程 replaceAll 找到了一个函数,但是这个:
var newData = data.replaceAll("\","");
..也没有区别。
尝试将 \" 替换为 ' 然后将 ' 替换为 " 只是 returns 我到 \"
var newData = data.replaceAll("\"","'");
now newData = [{'day':0,'periods':['0xffffffffffff']}]
newData = newData.replaceAll("'","\"");
and it's back to [{\"day\":0,\"periods\":[\"0xffffffffffff\"]}]
Trying to process with single quote, i.e. [{'day':0,'periods':['0xffffffffffff']}] gives me the same Uncaught TypeError message.
有什么办法可以解决这个问题吗?
谢谢。
错误是您在字符串上使用了 JSON.stringify
。仔细查看 this.responseText
- responseText - 你正在使用的 属性。只需将其更改为
var data = JSON.parse(this.responseText);
console.log("data=" + data);