我已经为这个问题苦恼了几个小时了。
我有一个简单的Node服务器,正在调用外部API,以获取(巨大的,大约4+ MB)JSON数据。我使用的请求模板是从Node文档中直接取出的,非常基础:
const muniURL = `http://api.511.org/transit/vehiclemonitoring?api_key=${API_KEYS.API_KEY_511}&format=json&agency=sf-muni`;
http.get(muniURL, (res) => {
const statusCode = res.statusCode;
const contentType = res.headers['content-type'];
console.log('Status Code:', statusCode);
console.log('Content Type:', contentType);
let error;
if (statusCode !== 200) {
error = new Error(`Request Failed.\n` +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(`Invalid content-type.\n` +
`Expected application/json but received ${contentType}`);
}
if (error) {
console.log(`Request error: ${error.message}`);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
console.log('parsedData:', parsedData);
} catch (e) {
console.log(`Caught error: ${e.message}`);
}
});
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
...每次,它都会命中catch
语句,并显示:Caught error: Unexpected token in JSON at position 0
。(请注意'token'和'in'之间有两个空格。)
我已经使用两个不同的基于Web的JSON验证器检查了Chrome和Postman返回的JSON,并且它被认为是有效的。而将rawData
写入文件时,看起来像是一个缓冲区(?)...
1fef bfbd 0800 0000 0000 0400 efbf bdef
bfbd efbf bd72 efbf bdc8 b62d efbf bd2b
0c3f 7547 1cef bfbd 00ef bfbd efbf bd0b
efbf bd5b 49ef bfbd 2def bfbd 6c6b efbf
bd5c 55ef bfbd efbf bd44 3fef bfbd 126c
71ef bfbd 021c 2029 6def bfbd 13ef bfbd
efbf bdef bfbd 437f 52ef bfbd 4227 48ef
bfbd efbf bd4d efbf bd31 13ef bfbd 09ef
bfbd 5d2f 7bef bfbd efbf bde5 aa81 745e
efbf bd65 efbf bd31 efbf bdef bfbd efbf
...
在这里Buffer.isBuffer
返回false。
到目前为止,我尝试了首先JSON.stringify
,toString
,转换为new Buffer
然后再序列化,去除空格.trim
以及替换各种转义字符,但都没有成功。
我错过了什么吗?
编辑:我意识到我正在验证由Chrome和Postman获取的JSON,它们显然正在进行某种预处理。 curl
URL会产生一堆肯定不是JSON的混乱。 我依然不知道那一堆混乱实际上是什么数据类型,以及为什么明确请求JSON时却没有得到JSON。
��ε�@+���@}<��ot�Xu���y��֙�%���"���٤��r�$�����ҙ��@2�+LDSX�ѥ~r���k�����fg�( ~J,�GS����T��$�'�����nc$XfI%1t!FO@)�>�ܪ��Z�Z��7q30�uV�衷�����ZrR3��4�>���*J�� ��<��͏����2o�&m�R�:�|p��>�[=��x}m��{���1�����E�������4�O�Q��+�rjZ�ȏ@��}��ź6ҪG�u��[�ڞ"�妕d|t��>�}+|z��>j\k�O�l��IY�d��Ӓ=0�vYTe]iޝ#SQb�9-l���6}U��[����?d���y&
我认为是 Chrome/Postman 进行了一些自动预处理,导致我被误导了。使用curl
命令访问该 URL 也会得到同样的非 JSON 消息。但我不知道我正在获取什么数据类型。 - dangerismycat