【发布时间】:2017-09-08 19:39:30
【问题描述】:
var http = require('http');
var optionsget = {
host : 'demo4712411.mockable.io',
port : 80,
path : '/Jobs',
method : 'GET'
};
console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
var reqGet = http.get(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
console.log("------- ID " + res.Lat);
var buffer='';
res.on('data', function(d) {
console.log(d);
buffer += d.toString();
});
res.on('end', function() {
console.info('GET result:\n');
console.log(buffer);
console.info('\n\nCall completed');
});
});
reqGet.on('error', function(e) {
console.error("Error: --" + e);
});
reqGet.end();
所以我得到了我需要的所有输出,并且我得到了 JSON 文件作为一个字符串 --
获取结果:
{
"Id" : 23
"OwnerrId" : 233
"ProviderId" : 2343
"Lat" : 342.23423
"Long" : 23.32233
"Address" : "234 Maybach way, Bellevue 98803"
}
但是,我无法访问 JSON 文件的组件(如 Id 和 OwnerrId)。我尝试了一些类似 JSON.parse(d) 的方法,但这给了我一个错误。不知道该怎么办。
【问题讨论】:
-
您的 JSON 响应不正确。在每个属性之间添加逗号: { "Id" : 23, "OwnerrId" : 233, "ProviderId" : 2343, ...
标签: javascript json node.js rest