[英]Node.JS JSON.parse UTF-8 issue
I've built a small script that allow me to get keywords suggestion from Google search API.我构建了一个小脚本,允许我从 Google 搜索 API 获取关键字建议。
The main problem is if the response contain special characters (like à é ù etc.): my application return me unreadable keywords like that: �,�a,�a va,� majuscule,�a marche,�,�a y est,�a film,�gag,�a il est revenu,�a va de soi,,[object Object]主要问题是响应是否包含特殊字符(如 à é ù 等):我的应用程序返回我这样不可读的关键字:�,�a,�a va,� majuscule,�a marche ,�,�a y est,�a film,�gag,�a il est revenu,�a va de soi,,[object Object]
Here's my Node.js Script:这是我的 Node.js 脚本:
var express = require('express');
var request = require('request');
var app = express();
app.get('/sug', function (req, res) {
var KW = req.query.KW ;
console.time("Délai");
var url = "http://clients1.google.fr/complete/search?hl=fr&q=" + KW + "&json=t&client=hp";
request(url, function (err, resp, body) {
body = JSON.parse(body);
res.end(body.toString());
console.timeEnd("Délai");
});
});
app.listen(1337);
console.log('Serveur : OK');
The call is easy to make, just type http://localhost:1337/sug?KW=ç in your browser.拨打电话很容易,只需在浏览器中输入http://localhost:1337/sug?KW=ç即可。
Do you know how to solve this and get the utf-8 working?你知道如何解决这个问题并让 utf-8 工作吗?
Like vmkcom said, it's because the response is using ISO-8859-1, you have to manually convert to utf-8.正如 vmkcom 所说,这是因为响应使用的是 ISO-8859-1,您必须手动转换为 utf-8。
iconv
package can help you with that: iconv
包可以帮助你:
var request = require('request');
var iconv = require('iconv');
var KW = 'ç' ;
console.time("Délai");
var url = "http://clients1.google.fr/complete/search?hl=fr&q=" + KW + "&json=t&client=hp";
var options = {
url: url,
encoding: null // << set encoding to null so request don't try to force utf-8
};
var ic = new iconv.Iconv('iso-8859-1', 'utf-8');
request(options, function (err, resp, body) {
// body is a Buffer not a string, convert to utf-8 buffer then to utf-8 string
body = ic.convert(body).toString('utf-8');
console.log(body);
console.timeEnd("Délai");
});
Thanks for all the help.感谢所有的帮助。 So i did this code using your feedbacks:
所以我使用您的反馈做了这段代码:
var express = require('express');
var request = require('request');
var iconv = require('iconv');
var app = express();
app.listen(1337);
console.log('Serveur : OK');
app.get('/sug', function (req, res) {
var KW = req.query.KW;
console.time("Délai");
var url = "http://clients1.google.fr/complete/search?hl=fr&q=" + KW + "&json=t&client=hp";
var options = {
url: url,
encoding: null // << set encoding to null so request don't try to force utf-8
};
var ic = new iconv.Iconv('iso-8859-1', 'utf-8');
request(options, function (err, resp, body) {
res.set({ 'content-type': 'application/json; charset=utf-8' });
body = ic.convert(body).toString('utf-8');
body = JSON.parse(body);
res.end(body.toString());
console.timeEnd("Délai");
});
});
It work great !效果很好!
http://localhost:1337/sug?KW=%C3%A7a http://localhost:1337/sug?KW=%C3%A7a
ça,ça,ça va,ça marche,ça y est,ça film,ça il est revenu,ça va de soi,ça te va,ça s'est bien passé,ça m'énerve,,[object Object] ça,ça,ça va,ça marche,ça y est,ça film,ça il est revenu,ça va de soi,ça te va,ça s'est bien passé,ça m'énerve,,[object Object]
response set响应集
response.set({ 'content-type': 'application/json; charset=utf-8' }); response.set({ 'content-type': 'application/json; charset=utf-8' });
app.use('/reverse',function (requsting,response) { request.get({ url: url, json: true, headers: {'User-Agent': 'request'} }, (err, res, data) => { if (err) { console.log('Error:', err); } else if (res.statusCode !== 200) { app.use('/reverse',function (requsting,response) { request.get({ url: url, json: true, headers: {'User-Agent': 'request'} }, (err, res, data ) => { if (err) { console.log('Error:', err); } else if (res.statusCode !== 200) {
response.end(JSON.stringify({status : 'error'}));
} else {
response.set({ 'content-type': 'application/json; charset=utf-8' });
response.end(JSON.stringify({status : 'ok','api' : 'website : https://homeandroid.ir','json':data}));
}
});
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.