这是我的代码,它基于flickr示例,因为我正在尝试学习如何使用JSON。 我的问题:
如何让var标题在HTML中正确显示,我仍然是编程尝试自学的新手。
我得到[对象对象],我认为这意味着它不是一个字符串。不知道如何把它写成一个,可能非常简单但超出我的范围。
谢谢!
$.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示例:我定位所有“item”子项
({
"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: Ran 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)"}
答案 0 :(得分:2)
如果你使用Firefox和Firebug,你可以这样做:
console.log(data);
在每个()调用中以查看数据的结构。 而你的title var应该在each()调用中是本地的。
var title = item.title;
或
var title = $(item).children().attr('title'); // depens on how the data is structured
答案 1 :(得分:0)
答案 2 :(得分:0)
我想通了把var放在每个内部,我能够得到它。工作