【问题标题】:How do display a var with json and jquery?如何使用 json 和 jquery 显示 var?
【发布时间】:2009-07-09 16:25:27
【问题描述】:

这是我的代码,它基于 flickr 示例,因为我正在尝试学习如何使用 JSON。 我的问题:

如何让 var 标题在 HTML 中正确显示,我还是新手,试图自学编程。

我得到 [object object] 我认为这意味着它不是字符串。不知道怎么写,可能真的很简单,但超出了我的理解。

谢谢!

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cats&tagmode=any&format=json&thumb_size=s&jsoncallback=?",
                function(data){
                    var title = $(this).children('.title');
              $.each(data.items, function(i,item){
                    var content = '<li>';
                    content += '<p class="title"> <a href="' + item.link + '">' + item.title + '</a></p>'
                    content += '<img width="133" height="100" class="prettyPhoto" src="' +  item.media.m + '"/>';
                    content += '<p class="date">' + title + '</p>'
                    content += '</li>';
                    $(content).appendTo("#recent-photos");
                    if ( i == 15 ) return false;
                  });
                });

更新:

我使用的 JSON 示例:我的目标是所有“项目”子项

({
        "title": "Recent Uploads tagged cats",
        "link": "http://www.flickr.com/photos/tags/cats/",
        "description": "",
        "modified": "2009-07-09T16:50:47Z",
        "generator": "http://www.flickr.com/",
        "items": [
       {
            "title": "Beautiful Downtown Gary, Indiana.",
            "link": "http://www.flickr.com/photos/fotomard/3704818882/",
            "media": {"m":"http://farm3.static.flickr.com/2506/3704818882_8a8e793e47_m.jpg"},
            "date_taken": "2009-06-27T17:21:05-08:00",
            "description": "<p><a href=\"http://www.flickr.com/people/fotomard/\">fotomard<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/fotomard/3704818882/\" title=\"Beautiful Downtown Gary, Indiana.\"><img src=\"http://farm3.static.flickr.com/2506/3704818882_8a8e793e47_m.jpg\" width=\"160\" height=\"240\" alt=\"Beautiful Downtown Gary, Indiana.\" /><\/a><\/p> <p>Backstage at the Palace Theater<\/p>",
            "published": "2009-07-09T16:50:47Z",
            "author": "nobody@flickr.com (fotomard)",
            "author_id": "51533956@N00",
            "tags": "urban cats house church wet beautiful nude theater downtown decay indiana palace crack abandon drugs gary dilapidation trap trespassing dilapitation"
       }

更新 2: 然json2.js

我得到:这是否意味着我需要对其进行解码,还是我只是定位错误?

{"length":0,"prevObject":{"length":1,"0":{"type":"GET","url":"http://api.flickr.com/services/feeds/photos_public.gne?tags=cats&tagmode=any&format=json&thumb_size=s&jsoncallback=jsonp1247160045746&_=1247160045957","data":null,"dataType":"script","global":true,"contentType":"application/x-www-form-urlencoded","processData":true,"async":true,"accepts":{"xml":"application/xml, text/xml","html":"text/html","script":"text/javascript, application/javascript","json":"application/json, text/javascript","text":"text/plain","_default":"*/*"},"cache":false}},"selector":".children(undefined)"}

【问题讨论】:

  • 对于那些感兴趣的人,上面的代码似乎改编自一个jquery教程:docs.jquery.com/Getjson。享受吧!

标签: jquery json


【解决方案1】:

如果您使用的是 Firefox 和 Firebug,您可以这样做:

console.log(data);

在each() 调用中查看数据的结构。 而且你的 title var 应该在 each() 调用中是本地的。

var title = item.title;

var title = $(item).children().attr('title'); // depens on how the data is structured

【讨论】:

    【解决方案2】:

    最好的办法是包含json2.js 文件并调用stringify():

    alert(JSON.stringify(data));
    

    这样您将看到 ajax 调用返回的内容。

    【讨论】:

      【解决方案3】:

      我想通了,通过将 var 放在 each 中,我能够得到它。工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        • 2011-09-05
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多