Node-Fetch - Danish Letters糟糕的JSON响应

时间:2017-05-10 19:36:03

标签: javascript json node.js rest escaping

我正在尝试在房地产网站上进行一些查询。

当我使用像'Næstved','Præstø',Karrebæksminde这样的字词进行抓取请求时,我得到了错误的回复,因为æ,ø等被?符号替换了???我试图逃避回应,它似乎有时会起作用......但我大多得到空洞的回应......

您可以尝试在此处进行查询:https://home.dk/umbraco/backoffice/home-api/BoligOrAddress/Boligdata?max=100&searchstring=N%C3%A6stved

以下是代码:

var cities = [
  'København',
  'København K',
  'Næstved'
];

var promises = cities.map(city => 
fetch(`https://home.dk/umbraco/backoffice/home-api/BoligOrAddress/Boligdata?max=1000000000&searchstring=${city}`)
.then(response => response.json())
.then(validate => { if(validate.Successed && validate.Status === 'OK') return validate })
.then(pushAlll => { pushedSuggestions.push(pushAlll); return pushAlll })

Promise.all(promises).then(results => {
    console.log(results);
});

问题是我得到了这样的结果{ suggest: 'N�stved', count: ''} 当我应该得到这个:

[
    {
    suggest: "Næstved kommune",
    count: "854"
    },
    {
    suggest: "Næstved, 4700",
    count: "406"
    },
    [
    {
    suggest: "Næstved Landevej, 4243 Rude",
    count: "1"
    },
    {
    suggest: "Næstved Landevej, 4250 Fuglebjerg",
    count: "1"
    },
    {
    suggest: "Næstvedvej, 4100 Ringsted",
    count: "2"
    },
    {
    suggest: "Næstvedvej, 4230 Skælskør",
    count: "3"
    },
    {
    suggest: "Næstvedvej, 4684 Holmegaard",
    count: "2"
    },
    {
    suggest: "Næstvedvej, 4720 Præstø",
    count: "4"
    },
    {
    suggest: "Næstvedvej, 4750 Lundby",
    count: "1"
    },
    {
    suggest: "Næstvedvej, 4760 Vordingborg",
    count: "4"
    }
]

1 个答案:

答案 0 :(得分:0)

谢谢你们!我没有将字母编码为UTF8,所以我最终使用编码器模块来做到这一点。

const utf8    = require('utf8')
fetch(`https://home.dk/umbraco/backoffice/home-api/BoligOrAddress/Boligdata?max=1000000000&searchstring=${utf8.encode(city)}`)

感谢您指出@Bergi

干杯!