使用双引号 (") 作为值将 JavaScript 对象转换为 Json
Coverting a JavaScript Object in to Json with a double quote(") as a value
我在转换对象时遇到问题
var obj={"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5" TXL/Qn/"};
在上面的对象中,我们在字符串中有一个类似于 6.5" 的值。
请帮帮我。
提前致谢。
转义如下:6.5\"
再说一次,那不是一个数组,而是一个对象
var obj={"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5\" TXL/Qn/"};
console.log(obj["SC Long Description"])
假设这是固定结构,您可以使用正则表达式捕获组,并在找到组 "SC Long Description"
时进行替换:
var str = '{"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5" TXL/Qn/"}'
var found = false;
str = str.replace(/(".*?")(?!\})/g, function(match) {
if (found && match.endsWith('"')) return match.substring(0, match.length - 1) + '\"';
found = found || match === '"SC Long Description"';
return match;
});
var obj = JSON.parse(str);
console.log(obj["SC Long Description"]);
你需要转义双引号,很简单。
var array1 = {
"Id": "1-AQC1Y1S",
"Root Order Item Id": "1-AQC1RSA",
"SC Long Description": "6.5\" TXL/Qn/"
};
console.log(array1["SC Long Description"]);
我在转换对象时遇到问题
var obj={"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5" TXL/Qn/"};
在上面的对象中,我们在字符串中有一个类似于 6.5" 的值。 请帮帮我。 提前致谢。
转义如下:6.5\"
再说一次,那不是一个数组,而是一个对象
var obj={"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5\" TXL/Qn/"};
console.log(obj["SC Long Description"])
假设这是固定结构,您可以使用正则表达式捕获组,并在找到组 "SC Long Description"
时进行替换:
var str = '{"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5" TXL/Qn/"}'
var found = false;
str = str.replace(/(".*?")(?!\})/g, function(match) {
if (found && match.endsWith('"')) return match.substring(0, match.length - 1) + '\"';
found = found || match === '"SC Long Description"';
return match;
});
var obj = JSON.parse(str);
console.log(obj["SC Long Description"]);
你需要转义双引号,很简单。
var array1 = {
"Id": "1-AQC1Y1S",
"Root Order Item Id": "1-AQC1RSA",
"SC Long Description": "6.5\" TXL/Qn/"
};
console.log(array1["SC Long Description"]);