簡體   English   中英

無法正確訪問API的響應

[英]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);

怎么會?

responsebranches屬性是一個數組:

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

因此,您必須訪問branches的第一個元素(這是您要查找的對象)以獲取version屬性:

response.branches[0].version

好吧, 分支沒有可以通過其名稱訪問的節點,只有一個數組,因此您不能通過名稱(沒有名稱)來獲取它,您必須執行以下操作:

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

索引數組和關聯數組不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM