所以,本质上我有一个fetch,接触到一个API,然后得到一个JSON对象作为回应。问题是,API会给单引号加上一个转义字符,例如
isn\'t
。这在JSON.parse()中引发了一个错误,所以我想在调用response.json()之前对响应的主体进行操作。所以这是我目前的代码。
httpCallout(){
fetch('/1.0/?method=getQuote&lang=en&format=json', {method: "GET", cache: "no-cache"})
.then(response => {
return response.json()
.then((data) => {
const quote = data.quoteText;
this.setState({
quote: quote,
author: data.quoteAuthor
this.colorChange(rgb);
问题是,每当调用response.json
,都有一个转义的单引号。我知道我可以抓住这个错误,什么都不做,但我更希望有一个解决方案,可以让我解析出转义的单引号,并且仍然可以从这个调用中设置我的状态。
从本质上讲,我想做的是,在运行response.json()之前,将一个带有类似于"It\'s tiring isn\'t it?"
的quoteText值的响应正确解析为"It's tiring isn't it?"
,这样就不会出现Bad escape character
的错误。