您可以手动替换它们中的每一个。
.filter("decoder", function() {
return function (item) {
return item
.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}
})
我认为这个列表涵盖了所有这些,但是如果您看到其他东西,您可以随时添加它。它仍然有点混乱,但我认为它比向DOM添加元素好。
UPDATE
如果你正在寻找一个更完整的解决方案,可以做到以下几点:
.filter("decoder", function() {
return function (item) {
// it would be better to define this globally as opposed to within the function
var ENTITIES = {
'&': '&',
'&': '&',
'&apos': '\'',
''': '\'',
'>': '>',
'>': '>',
'<': '<',
'<': '<',
'"': '"',
'"': '"'
};
return item.replace(/&#?[0-9a-zA-Z]+;?/g, function (entity) {
if (entity.charAt(1) === '#') { // if it's a numeric entity
var code;
if (entity.charAt(2).toLowerCase() === 'x') { // if it's a hex code
code = parseInt(entity.substr(3), 16);
} else {
code = parseInt(entity.substr(2));
}
if (isNaN(code) || code < -32768 || code > 65535) { // if it's not a valid numeric entity
return '';
}
return String.fromCharCode(code);
}
return ENTITIES[entity] || entity;
});
}
});
正如你可以看到,这个解决方案是显著更复杂,但它确实覆盖正常的实体以及所有数字的实体。如果您的目标是在未来避免更新,这是您最好的选择。
JSON *实际*看起来像什么?当你说“逃脱”时,你的意思是用HTML语法如“'”或其他什么来逃脱? – Pointy
对不起,它看起来像解码它,当我把它。我现在正确更新 –
如果可能,修复服务返回*实际*信息:<使用HTML/[字符编码](http:// en。 wikipedia.org/wiki/Character_encodings_in_HTML)在适当的上下文之外是错误的。 – user2864740