admin管理员组文章数量:1404922
I've built a small script that allow me to get keywords suggestion from Google search 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]
Here's my Node.js Script :
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 = ";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.
Do you know how to solve this and get the utf-8 working ?
I've built a small script that allow me to get keywords suggestion from Google search 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]
Here's my Node.js Script :
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/plete/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.
Do you know how to solve this and get the utf-8 working ?
Share Improve this question edited Dec 21, 2015 at 12:12 Arnaud Boyer asked Dec 21, 2015 at 11:23 Arnaud BoyerArnaud Boyer 231 gold badge1 silver badge4 bronze badges 5- The URL you posted is a network local URL, and therefore not accessible for anyone not on your network (i.e. us). – Tiddo Commented Dec 21, 2015 at 11:34
-
1
problem with clients1.google.fr response. It's encoded with "ISO-8859-1" and it returns
["ç",["�","�a","� encoding","�in takvimi","�eyrek alt�n fiyat�","��plak k�zlar","�a��atay ulusoy","��plak kad�nlar","�ukurova �1\u20444niversitesi","�ocuk nas�l yap�l�r"],[],{"google:suggesttype":["QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY"]}]
But after convert into UTF-8 we get that strange symbols ¿½,�a – vmk Commented Dec 21, 2015 at 12:37 - @vmk So why this work in this URL : clients1.google.fr/plete/… ?? – Arnaud Boyer Commented Dec 21, 2015 at 12:41
- 1 @Tiddo Yes, you must to install node.js and run my script to see it on your browser ;) – Arnaud Boyer Commented Dec 21, 2015 at 12:42
-
Because your browser is honouring the
Content-Type
HTTP response header and is interpreting the encoding correctly. – deceze ♦ Commented Dec 21, 2015 at 12:42
3 Answers
Reset to default 1Like vmk said, it's because the response is using ISO-8859-1, you have to manually convert to utf-8. iconv
package can help you with that:
var request = require('request');
var iconv = require('iconv');
var KW = 'ç' ;
console.time("Délai");
var url = "http://clients1.google.fr/plete/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/plete/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
ç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' });
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}));
}
});
});
本文标签: javascriptNodeJS JSONparse UTF8 issueStack Overflow
版权声明:本文标题:javascript - Node.JS JSON.parse UTF-8 issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744873941a2629801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论