2013-11-14 4 views
3

저는 RaspberryPi에서 NodeJS를 사용하는 학교 프로젝트 용 전기 엔지니어입니다. 필자는 객체 지향 프로그래밍에 대한 아이디어에 익숙하지 않고 JSON 객체에서 특정 값을 가져 오는 데 문제가 있습니다.JSON에서 값을 얻을 수 없습니다

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

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

그것은 다음과 같은 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 
    } 
] 

난에서 "값"을 얻으려고을

내 프로그램은 다음 코드를 사용하여 객체 "결과"를 반환 울프 럼 알파를 조회 첫 번째 하위. 나는 시도 : var newResults = result.subpods[0].value;

그러나 이것은 나에게 오류를 준 : TypeError: Cannot read property '0' of undefined

내가 적어도 지난 시간 동안 서로 다른 조합을 시도했습니다. 도와주세요!

은, 시간 내 주셔서 감사

Bobbyg

+1

: http://stackoverflow.com/questions/5726729/ how-to-parse-json-using-nodejs – Jack

답변

9

result 배열처럼 보인다.

시도 :

var newResults = result[0].subpods[0].value; 
+0

정말 고마워요! – bobbyg603

4

다음 코드를보십시오 ..

당신은 객체로 돌려 위해 JSON을 구문 분석 할 필요가
var newResults = result[0].subpods[0].value; 
+0

정말 고마워요! – bobbyg603

관련 문제