对于我的node.js express app,我使用Request模块通过REST API请求数据。
这是我在 app.js
中的请求实施var request = require('request')
request.post('https://getpocket.com/v3/get', {
headers: {'content-type':'application/json'},
body: JSON.stringify({
consumer_key:'...',
access_token:'...',
contentType:'article',
sort:'title'
})
}, function (err, res, body) {
console.log(JSON.parse(body))
})
我正在获得JSON响应,如下所示
{ status: 1,
complete: 1,
list:
{ '890245271':
{ item_id: '890245271',
resolved_id: '890245271',
given_url: 'http://davidwalsh.name/open-graph-data-nodejs',
given_title: 'Get Open Graph Data with Node.js',
... }
},
error: null,
search_meta: { search_type: 'normal' },
since: 1444630917 }
问题是这是无效的JSON,因为JSON属性需要是字符串。我在这里缺少什么?
答案 0 :(得分:3)
您正在解析 JSON:
console.log(JSON.parse(body))
// Here ----^^^^^^^^^^^^^^^^
因此,您所看到的是解析JSON所产生的JavaScript对象的console.log
表示。
如果您想要查看JSON,请不要解析:
console.log(body);