阅读json,直到“更多”评论
问题描述:
我想从tumblr博客拉,并使用JavaScript在另一个网页上显示它。阅读json,直到“更多”评论
我使用的是$ TUMBLR_BLOG/api/read/json feed,它提供了一个充满博客文章信息的变量。
我想打印一切,直到"<!-- more -->"
字符集的'正规体'部分,即。我不想在“常规体”中打印所有内容,直到更多部分。
有关如何做到这一点的想法?
例如, API阅读:http://blog.intercut.yegfilm.ca/api/read/json
例如。我正在使用的基本代码:
<script type="text/javascript" src="http://blog.intercut.yegfilm.ca/api/read/json"></script>
<script type="text/javascript">
// The variable "tumblr_api_read" is now set.
document.write(
'<h3> <a href="' + tumblr_api_read.posts[0]['url'] + '">' + tumblr_api_read.posts[0]['regular-title'] + '</a></h3>' +
tumblr_api_read.posts[0]['regular-body']);
</script>
答
<script type="text/javascript">
// The variable "tumblr_api_read" is now set.
var url = tumblr_api_read.posts[0]['url'];
var title = tumblr_api_read.posts[0]['regular-title'];
var body = tumblr_api_read.posts[0]['regular-body'];
body = body.substring(0,body.indexOf("<!-- more -->"));
document.write('<h3> <a href="' + url + '">' + title + '</a></h3>' + body);
</script>
这么简单:)
答
有点像responseData.split("<!-- more -->")[0]
?
答
获取<!-- more -->
的索引并打印到该索引的子串。
sampleString = "Foo <!-- more --> Bar";
moreIndex = sampleString.indexOf("<!-- more -->");
if (moreIndex > 0) {
console.log(sampleString.substring(0, moreIndex));
}
答
使用JavaScript SubString()
方法:
例:
var mystring = 'Hello World <!-- more -->';
alert(mystring.substring(0,mystring.indexOf("<!-- more -->")));
的jsfiddle:http://jsfiddle.net/8CXd8/