繁体   English   中英

使用双引号(“)作为值将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"]); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM