返回的JSON属性不是我的node.js表达应用程序

时间:2015-10-12 06:26:44

标签: javascript json node.js

对于我的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属性需要是字符串。我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

您正在解析 JSON:

console.log(JSON.parse(body))
// Here ----^^^^^^^^^^^^^^^^

因此,您所看到的是解析JSON所产生的JavaScript对象的console.log表示。

如果您想要查看JSON,请不要解析:

console.log(body);