在JavaScript中使用波兰语字符导入JSON时编码错误 [英] Wrong encoding when importing JSON with Polish characters in JavaScript
问题描述
我有以下JSON文件 locations.json:
{
lubelskie:[
abramów,
adamów,
aleksandrów,
annopol,
baranów,
batorz ,
bełżec,
bełżyce
]
}
我使用以下语句将JSON导入到我的Class中:
从 ./locations.json导入位置;
类区域{
Constructor(){
console.log(locations);
}
}
出口默认区域;
我得到的控制台输出如下:
{
lubelskie:[abramów,adamów,aleksandrów, annopol,baranów, batorz,beÅ,żec, bełżyce]
}
问题是字符是从abramów中得到编码的。从bełżyce到abramów 或 我无法以其原始编码显示它们。 JSON文件已编码 我有以下package.json软件包: 我尝试使用原始加载程序以不同的方式加载文件。并将UTF-8字符转换为/ uABC,但是看起来好像在import语句期间进行了编码。 babel编译器保持正确的编码,转换后的webpack文件具有正确的UTF-8编码,但是当脚本运行时,编码发生。 我在做错什么的任何建议? / p> 更新#1: 我试图将JSON文件编码为Unicode: 我尝试使用 https://www.npmjs.com/package/unidecode 库和head / script标签中的meta charset标签,但没有任何效果。我仍然在控制台中得到相同的输出。 更新#2: 我尝试将JSON文件更改为包含以下内容的JS文件: 然后导入文件,如下所示: 我仍然在控制台中获得相同的输出,位置已编码。 我意识到问题出在import语句上,由于某种原因,导入对我的JSON数据进行了编码。我终于想出了一个可行的解决方案-使用AJAX调用加载JSON文件。 JSON文件 data.json: AJAX请求: 控制台输出中的JSON数据与JSON输入文件中的JSON数据相同,就像我想要的一样。不进行任何编码,并且在不包含任何 I have the below JSON file "locations.json": I import the JSON in to my Class, using the below statement: The console output I get is below: The problem is the characters get encoded from "abramów" to "abramów" or from "bełżyce" to "bełżyce". I am unable to show them in their original encoding. The JSON file is encoded in UTF-8 format. I have the below package.json packages: I tried loading the files in different ways, using "raw-loader" and converting the UTF-8 characters to /uABC, but it looks like the encoding happens during the import statement. The babel transpiler keeps the encoding correct and the converted webpack files have the correct UTF-8 encoding, but when the script runs the encoding happens. Any suggestions where I am doing something wrong? UPDATE #1: I tried to encode the JSON file to Unicode: I tried to use the https://www.npmjs.com/package/unidecode library and the meta charset tags in the head/script tag, but nothing worked. I still get the same output in the console. UPDATE #2: I tried changing the JSON file to a JS file with the below contents: Then importing the file like below: I still get the same output in the console, the locations are encoded. I realized the issue was with the import statement and that for some reason the importing encodes my JSON data. I finally come up with a working solution - loading the JSON file using an AJAX call. JSON file "data.json": AJAX request: The JSON data in the console output is the same as in the JSON input file, exactly like I wanted. No encoding occured and inserting the data in to a HTML element also worked without including any I am still unsure why the importing encoded the JSON data, if someone can suggest a fix I would be greatful, but for now this solution works. 这篇关于在JavaScript中使用波兰语字符导入JSON时编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
devDependencies :{
@ babel / core: ^ 7.6.4,
@ babel / plugin-syntax-dynamic-import: ^ 7.0.0&,
@ babel / preset-env: ^ 7.6.3,
acorn: ^ 6.3.0&,
autoprefixer: ^ 9.6 .5,
babel-loader: ^ 8.0.6,
clean-webpack-plugin: ^ 0.1.19,
copy-webpack-plugin: ^ 5.0.4,
css-loader: ^ 1.0.0&,
file-loader: ^ 2.0。 0,
imagemin: ^ 6.0.0&,
img-loader: ^ 3.0.0&,
lodash: ^ 4.17.15,
mini-css-extract-plugin: ^ 0.4.2,
node-sass: ^ 4.12.0,
postcss-loader: ; ^ 3.0.0&,
原始加载程序: ^ 4.0.1,
sass-loader: ^ 7.3.1,
; tar: ^ 4.4.13,
url-loader: ^ 1.1.1,
utf8: ^ 3.0.0&,
webpack: ^ 4.41.2,
webpack-cli: ^ 3.3.9,
zip-webpack-plugin: ^ 3.0.0&;
},
resolutions:{
webpack / acorn: 6.1.1,
tar: = 4.4。 2;
}
abram\u00f3w,
adam\u00f3w,
aleksandr\u00f3w ;
annopol,
baran\u00f3w,
batorz,
be\u0142\u017cec,
be\u0142\u017cyce;
导出默认JSON.stringify({
lubelskie ;:[[
abramów,
adamów,
aleksandrów,
annopol,
baranów,
batorz,
bełżec,
bełżyce
]
})
从 ./locations.js导入位置;
类区域{
Constructor(){
// const l = JSON.parse(locations);
console.log(locations);
}
}
出口默认区域;
{
lubelskie:[
abramów,
adamów,
aleksandrów,
annopol,
baranów,
batorz,
bełżec,
bełżyce
]
}
类区域{
构造函数(){
Axios.get( data.json,函数(响应){
console.log(response);
},function(response){
console.log( Error:" ;, response);
});
}
}
出口默认区域;
chartset = utf-8
元标记的情况下,将数据插入HTML元素也可以正常工作。 p>我仍然不确定导入为什么要对JSON数据进行编码,如果有人可以提出修复建议,我将非常满意,但目前此解决方案有效。{
"lubelskie": [
"abramów",
"adamów",
"aleksandrów",
"annopol",
"baranów",
"batorz",
"bełżec",
"bełżyce"
]
}
import locations from "./locations.json";
class areas {
constructor() {
console.log(locations);
}
}
export default areas;
{
lubelskie: ["abramów", "adamów", "aleksandrów", "annopol", "baranów", "batorz", "bełżec", "bełżyce"]
}
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.6.3",
"acorn": "^6.3.0",
"autoprefixer": "^9.6.5",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^0.1.19",
"copy-webpack-plugin": "^5.0.4",
"css-loader": "^1.0.0",
"file-loader": "^2.0.0",
"imagemin": "^6.0.0",
"img-loader": "^3.0.0",
"lodash": "^4.17.15",
"mini-css-extract-plugin": "^0.4.2",
"node-sass": "^4.12.0",
"postcss-loader": "^3.0.0",
"raw-loader": "^4.0.1",
"sass-loader": "^7.3.1",
"tar": "^4.4.13",
"url-loader": "^1.1.1",
"utf8": "^3.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.9",
"zip-webpack-plugin": "^3.0.0"
},
"resolutions": {
"webpack/acorn": "6.1.1",
"tar": ">=4.4.2"
}
"abram\u00f3w",
"adam\u00f3w",
"aleksandr\u00f3w",
"annopol",
"baran\u00f3w",
"batorz",
"be\u0142\u017cec",
"be\u0142\u017cyce"
export default JSON.stringify({
"lubelskie": [
"abramów",
"adamów",
"aleksandrów",
"annopol",
"baranów",
"batorz",
"bełżec",
"bełżyce"
]
})
import locations from "./locations.js";
class areas {
constructor() {
//const l = JSON.parse(locations);
console.log(locations);
}
}
export default areas;
{
"lubelskie": [
"abramów",
"adamów",
"aleksandrów",
"annopol",
"baranów",
"batorz",
"bełżec",
"bełżyce"
]
}
class areas {
constructor() {
Axios.get("data.json", function(response) {
console.log(response);
}, function(response) {
console.log("Error: ", response);
});
}
}
export default areas;
chartset="utf-8"
meta tags.