SyntaxError:意外的令牌&在JSON中 [英] SyntaxError: Unexpected token & in JSON

查看:114
本文介绍了SyntaxError:意外的令牌&在JSON中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 $ http.get()来执行GET请求,响应是JSON,但对于某些字符使用HTML编码,一个例子是如下所示,用编码& quot;

I'm using the $http.get() to do a GET request, the response is in JSON, but with HTML encoding for some characters, one example is as below, " is encoded with "

{
    "description":"invalid",
    "errorCode":"error.loginInvalid"
}

我还在使用 $ httpProvider.interceptors.push('httpErrorsInterceptor') ; ,httpErrorsInterceptor将在 responseError 中显示错误信息。由于响应JSON未正确解码,当尝试处理它时,它将显示 SyntaxError:控制台中的JSON 中出现意外的令牌和放大器。我想知道在处理时我们如何解码响应 JSON ? code> $ httpProvider ?

Also i'm using $httpProvider.interceptors.push('httpErrorsInterceptor');, httpErrorsInterceptor will show the error information in responseError. Since the response JSON is not decoded properly, when try to process it, it will show SyntaxError: Unexpected token & in JSON in console. I wonder how can we decode the response JSON when processing? in $httpProvider?

推荐答案

将html实体转换为文本的一种方法是创建一个dom元素,注入s将其作为html并将其作为文本检索回来。 DOM解析器在转换方面做得比编写自己的更好

One way to convert html entities to text is to create a dom element , inject the string as html and retrieve it back as text. The DOM parser will do better job of converting than writing your own

var str='{ "description":"invalid", "errorCode":"error.loginInvalid&quot"}'
var JSON = angular.element('<div>').html(str).text();

但由于上一个值的额外报价,显示的示例仍然是无效的JSON

But example shown is still invalid JSON due to extra quote on last value

上一次的工作演示& quot 已删除

这篇关于SyntaxError:意外的令牌&amp;在JSON中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆