[英]Can't access response from API properly
我的API返回此response
:
{"__v":0,"short":"8xdn4a5k","_id":"5404db5ac27408f20440babd","branches":[{"version":1,"code":""}],"ext":"js","language":"javascript"}
這有效:
console.log(response.short);
但這會產生undefined
:
console.log(response.branches.version);
怎么會?
response
的branches
屬性是一個數組:
"branches":[{"version":1,"code":""}]
因此,您必須訪問branches
的第一個元素(這是您要查找的對象)以獲取version
屬性:
response.branches[0].version
好吧, 分支沒有可以通過其名稱訪問的節點,只有一個數組,因此您不能通過名稱(沒有名稱)來獲取它,您必須執行以下操作:
console.log(response.branches[0].version);
索引數組和關聯數組不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.