【发布时间】:2011-02-18 18:30:25
【问题描述】:
我正在尝试通过 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"
]
}
我无法返回“regular-title”和“regular-body”。
我的 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 里面有破折号,但我找不到任何关于如何使它在这种情况下工作的文档!
谢谢。
【问题讨论】:
-
posts是一个数组;不要使用for ... in循环数组,使用for(i = 0; i < posts.length; i += 1)。另请参阅此问题:stackoverflow.com/questions/500504/…
标签: javascript json tumblr