[英]Correctly display special characters in JSON API response using nodeFetch
我有一個通過HTTP與RESTful API通信的反應應用程序。 API返回一個可能包含特殊字符的JSON響應主體。
示例響應可能如下所示:
{
"name": "Property One",
"description": "Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) ±100ppm/°C Pad SMD Automotive T/R",
...
}
所討論的特殊字符是(但不限於)±和°。
我使用nodeFetch
執行請求, nodeFetch
所示:
nodeFetch(url, { method: 'GET', headers }).then(response => response.json());
如果我在使用response.json()
反序列化后觀察對象,則description
屬性現在如下所示:
"Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) �100ppm/�C Pad SMD Automotive T/R"
如何阻止特殊字符替換為 ?
你也可以看一下這個問題,可能cskau-g repost應該可以幫到你https://github.com/dart-lang/http/issues/175
我認為問題是編碼為utf-8,內容類型必須設置為text / html
您可能應該設置標題: Content-Type
為text/html; charset=utf8
text/html; charset=utf8
。 nodeFetch
支持開箱即用的UTF-8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.