2014-09-01 61 views
0

我的API返回此response響應:無法訪問從API正確

{"__v":0,"short":"8xdn4a5k","_id":"5404db5ac27408f20440babd","branches":[{"version":1,"code":""}],"ext":"js","language":"javascript"} 

這工作:

console.log(response.short); 

但這會產生undefined

console.log(response.branches.version); 

怎麼來的?

+4

分支是一個數組,嘗試'response.branches [0] .version' – Rooster 2014-09-01 20:53:22

+0

謝謝 - 傻我! – tommyd456 2014-09-01 20:55:06

回答

1

responsebranches屬性是一個數組:

"branches":[{"version":1,"code":""}]

所以,你要訪問的branches的第一個元素(這是你要找的對象)來獲取version屬性:

response.branches[0].version 
0

好,分支沒有節點,可以是通過接觸它的名字,只有一個數組,所以你不能用一個名字得到它(不具備),你個人有T o不要:

console.log(response.branches[0].version); 

索引關聯陣列是不同的。