jQuery获取json值
我正试图通过jQuery json获取我的系统的最后3篇博客文章,其中draft=false,如果在网上找到了一些教程,但运气不佳,请使用以下键: “鼻涕虫”jQuery获取json值,jquery,json,Jquery,Json,我正试图通过jQuery json获取我的系统的最后3篇博客文章,其中draft=false,如果在网上找到了一些教程,但运气不佳,请使用以下键: “鼻涕虫” “已更新” “标题” “内容” “草稿=假” 下面是json输出 { "posts":[ { "Blog":{ "id":"1252", "client_id":"1432", "slug":"my-blog-slug
“已更新”
“标题”
“内容”
“草稿=假” 下面是json输出
{
"posts":[
{
"Blog":{
"id":"1252",
"client_id":"1432",
"slug":"my-blog-slug",
"last_updated_by":"614",
"draft":false,
"created":"2014-09-17 11:18:39",
"updated":"2014-09-17 11:18:39",
"locale":"isl",
"title":"This is the blog title",
"content":"<p>My excellent content.<\/p>",
"author":"John Doe"
},
"UpdatedBy":{
"id":"614",
"name":"John Doe"
}
}
]
}
{
“职位”:[
{
“博客”:{
“id”:“1252”,
“客户id”:“1432”,
“slug”:“我的博客slug”,
“上次更新者”:“614”,
“草稿”:错误,
“已创建”:“2014-09-17 11:18:39”,
“更新”:“2014-09-17 11:18:39”,
“语言环境”:“isl”,
“标题”:“这是博客标题”,
“内容”:“我的精彩内容。”,
“作者”:“约翰·多伊”
},
“更新者”:{
“id”:“614”,
“姓名”:“约翰·多伊”
}
}
]
}
我目前的剧本如下,但我很确定我做了很多错事
<script>
$(document).ready(function() {
$.ajax({
url: "blogs.json",
dataType: "text",
success: function(data) {
var json = $.parseJSON(data);
$('#results').html('Title: ' + json.created + '<br />Description: ' + json.content);
}
});
});
</script>
<div id="results"></div>
$(文档).ready(函数(){
$.ajax({
url:“blogs.json”,
数据类型:“文本”,
成功:功能(数据){
var json=$.parseJSON(数据);
$('#results').html('Title:'+json.created+'
Description:'+json.content);
}
});
});
任何帮助都将不胜感激。试试这个:posts是json对象中的第一个键,可以有多个,所以使用索引访问它,这里是零。获取帖子后,通过其键访问每个元素
var blog = json.posts[0]["Blog"];
$('#results').html('Title: ' + blog["created"] + '<br />Description: ' + blog["content"]);
var blog=json.posts[0][“blog”];
$('#results').html('标题:'+blog[“created”]+'
说明:'+blog[“content”]);
编辑-根据OP注释,添加以下代码
$(document).ready( function() {
$.parseJSON("/blogs.json", function(data){
var json = $.parseJSON(data);
var blog = json.posts[0]["Blog"];
$.each(blog, function(){
$("div#posts").append("<h1>Title: "+blog['title']+"</h1> <div class=\"post\">Content: "+blog['content']+"</div> <br />");
});
});
$(文档).ready(函数(){
$.parseJSON(“/blogs.json”),函数(数据){
var json=$.parseJSON(数据);
var blog=json.posts[0][“blog”];
$.each(blog,function(){
$(“div#posts”)。追加(“标题:+blog['Title']+”内容:+blog['Content']+”
);
});
});
尝试以下方法:
JS
$(document).ready(function() {
$.ajax({
url: "blogs.json",
dataType: "json",
success: function(data) {
var json = $.parseJSON(data);
$.each(json.posts, function(){
$('#results').html('Title: ' + this.Blog.title + '<br />Description: ' + this.Blog.content);
});
}
});
});
JS
$(文档).ready(函数(){
$.ajax({
url:“blogs.json”,
数据类型:“json”,
成功:功能(数据){
var json=$.parseJSON(数据);
$.each(json.posts,function(){
$('#results').html('Title:'+this.Blog.Title+'
说明:'+this.Blog.content);
});
}
});
});
帖子是数组,所以要访问第一个项目,您需要使用索引,例如:$('#results').html('Title:'+json.posts[0].Blog.created+'
说明:'+json.posts[0].Blog.content);或者,我的代码仍然无法实现此功能…是否有更完整的示例?。谢谢,非常感谢您在这方面提供的帮助…很抱歉,我对这一点非常陌生,我正在尝试以这种方式发布每3篇最新的博文。post.title
12/12/2014
发布内容我的代码现在看起来像这样$(document).ready(function(){$.parseJSON(“/blogs.json”)、function(data){$.each(posts[0][“Blog”]、function(){$(“div#posts”).append(“Title:“+Blog['Title']+”Content:“+Blog['Content']+”;”;}”);现在它只输出“););