簡體   English   中英

使用javascript獲取JSON數據(從Tumblr)

[英]Using javascript get JSON data (from Tumblr)

我正在嘗試通過JSON API閱讀Tumblr帖子。

使用他們的API,數據像這樣傳回:

var tumblr_api_read= {
 "posts":[
   "id":"1234",
   "regular-title":"This is the title of the post",
   "regular-body":"This is the body of the post"
 ]
}

我在返回“常規標題”和“常規正文”時遇到麻煩。

我的JS代碼如下所示:

var tumblr= tumblr_api_read['posts'];
for (eachPost in tumblr)
{
 var id=posts[eachPost].id; //Works fine
 var regularTitle=posts[eachPost].regular-title; //Returns NaN
}

我認為這是因為posts [eachPost] .regular-title中包含破折號,但找不到任何文檔說明如何在這種情況下工作!

謝謝。

javascript變量名稱中不能包含特殊字符,例如- 如果您的屬性名稱中包含特殊字符,則訪問它的唯一方法是使用[]方法訪問上面的posts對象,該方法為您提供了將屬性名稱作為字符串傳遞的選項。

var regularTitle = posts[eachPost]['regular-title'];

為了進一步闡述,您實際上是在以點表示法引用regular-title ,就像您將其作為對象一樣。 但這實際上是一個數組,這就是即使屬性值如上所述沒有破折號也不起作用的原因。

暫無
暫無

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

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