[英]How to remove " from my Json in javascript?
I am trying to inject json into my backbone.js app.我正在尝试将 json 注入到我的backbone.js 应用程序中。 My json has
"
我的 json 有
"
for every quote.对于每一个报价。
Is there a way for me to remove this?有没有办法让我删除它?
I've provided a sample below:我在下面提供了一个示例:
[{"Id":1,"Name":"Name}]
Presumably you have it in a variable and are using JSON.parse(data);
大概你把它放在一个变量中并且正在使用
JSON.parse(data);
. . In which case, use:
在这种情况下,请使用:
JSON.parse(data.replace(/"/g,'"'));
You might want to fix your JSON-writing script though, because "
不过,您可能想要修复您的 JSON 编写脚本,因为
"
is not valid in a JSON object.在 JSON 对象中无效。
Accepted answer is right, however I had a trouble with that.接受的答案是正确的,但是我遇到了麻烦。 When I add in my code, checking on debugger, I saw that it changes from
当我添加代码时,检查调试器,我看到它从
result.replace(/"/g,'"')
to到
result.replace(/"/g,'"')
Instead of this I use that:而不是这个,我用那个:
result.replace(/("\;)/g,"\"")
By this notation it works.通过这种表示法,它起作用了。
var data = $('<div>').html('[{"Id":1,"Name":"Name}]')[0].textContent;
这应该解析您需要的所有编码值。
The following works for me:以下对我有用:
function decodeHtml(html) {
let areaElement = document.createElement("textarea");
areaElement.innerHTML = html;
return areaElement.value;
}
i used replace feature in Notepad++ and replaced "
我在 Notepad++ 中使用了替换功能并替换了
"
(without quotes) with "
and result was valid json (不带引号)带
"
并且结果是有效的 json
在我的情况下,“quot”被替换为其他字符,所以我找到了一个愚蠢但有效的解决方法
str.replace(/&qu/g,'').replace(/ot\;/g,'')
This is a simple way to replace quot with what you need to change it - chars, strings ect.这是一种用您需要更改的内容替换 quot 的简单方法 - 字符、字符串等。
function solve(input) {
const replaced = '"' // e.g. replace " by "
const result = input.replace(/"/g, replaceWith)
return result;
}
console.log(solve('{"x":"1","y":"2","z":"10"}')
IF you are using SQL then you can use:如果您使用的是 SQL,那么您可以使用:
update tablename set payload = replace(payload, '&', chr(39)) where id = 'unique id'; update tablename set payload = replace(payload, '&', chr(39)) where id = 'unique id';
If you are using python then you can use:如果您使用的是 python,那么您可以使用:
import html
l = [{"Id":1,"Name":"Name}]
r = html.unescape(l)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.