jQuery获取JSON值 [英] jQuery get json values

查看:89
本文介绍了jQuery获取JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jQuery json为我的系统获取草稿= false 的最后 3篇博客文章,并遵循一些在线找到但没有运气的教程,并使用以下键: /p>

"sl"
已更新"
标题"
内容"
草稿=假"

这是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"
         }
      }
   ]
}

我当前的脚本在下面,但是我很确定自己做错了很多事.

<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>

任何帮助将不胜感激.

解决方案

尝试这种方式:

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);
            });
        }
    });
});

演示

I'm trying to fetch the last 3 blog posts with the draft = false for my system via jQuery json and following some tutorials if found online but without luck, and with the following keys:

"slug"
"updated"
"title"
"content"
"draft = false"

Here is the json output

{  
  "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"
         }
      }
   ]
}

My current script is below, but i'm pretty sure that i'm doing a lot of things wrong.

<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>

Any help would be so much appreciated.

解决方案

try this way:

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);
            });
        }
    });
});

Demo

这篇关于jQuery获取JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆