无法从JSON获得价值

bob*_*603 3 javascript json node.js

我是一名电气工程师,在RaspberryPi上使用NodeJS为学校开展项目.我对面向对象编程的想法很新,并且无法从JSON对象获取特定值.

我的程序查询wolframalpha,它使用以下代码返回一个对象"result":

var wolfram = require('wolfram').createClient("[CENSORED]")

wolfram.query("integrate 2x", function(err, result) {
  if(err) throw err
  console.log("Result: %j", result)
})
Run Code Online (Sandbox Code Playgroud)

它返回以下JSON:

[
  {
    "subpods": 
      [{
      "title":"",
      "value":" integral 2 x dx = x^2+constant",
      "image":"http://www5a.wolframalpha.com/Calculate/MSP/MSP36002050fgg595dgib5a000031a456025754352g?MSPStoreType=image/gif&s=59"
      }],
    "primary":true
  },
  {
      "subpods": [{
      "title":"",
      "value":"",
      "image":"http://www5a.wolframalpha.com/Calculate/MSP/MSP36012050fgg595dgib5a000055e24iecig9cc4ga?MSPStoreType=image/gif&s=59"
    }],
    "primary":false
  }
]
Run Code Online (Sandbox Code Playgroud)

我试图从第一个subpod获得"价值".我试过了: var newResults = result.subpods[0].value;

但这给了我一个错误: TypeError: Cannot read property '0' of undefined

至少在最后一小时我一直在尝试不同的组合.请帮忙!

感谢您的时间,

Bobbyg

小智 9

result 看起来像一个数组.

尝试:

var newResults = result[0].subpods[0].value;
Run Code Online (Sandbox Code Playgroud)