o = {
"photos": {
"page": 1,
"pages": 46,
"perpage": 5,
"total": "230",
"photo": [{
"id": "6643924777",
"owner": "34653895@N08",
"secret": "3b7c2f6469",
"server": "7153",
"farm": 8,
"title": "Huevos rellenos de bonito del norte Serrats",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0},
{
"id": "6371789575",
"owner": "53643555@N08",
"secret": "dda25093e0",
"server": "6105",
"farm": 7,
"title": "ALBÓNDIGAS DE CARNE",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0},
{
"id": "6200723807",
"owner": "17738873@N00",
"secret": "bf4fc5c84f",
"server": "6159",
"farm": 7,
"title": "albóndigas en salsa",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0},
{
"id": "6201239628",
"owner": "17738873@N00",
"secret": "def72cc124",
"server": "6128",
"farm": 7,
"title": "albóndigas en salsa",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0},
{
"id": "6171324677",
"owner": "34653895@N08",
"secret": "9f7a7489f5",
"server": "6161",
"farm": 7,
"title": "Pastel de patata con bonito del norte de Conservas Serrats y bechamel",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0}]}
}
这是查询的flickr API json结果 我喜欢这个
for (var key in o) {
for (var key1 in o[key]) {
var current = o[key][key1].id;
console.log(key1); //returns the pages information
console.log(current); //returns null
}
}
基本上我需要用这种格式构建图像
<img src="http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg" />
答案 0 :(得分:5)
for (var i = 0; i < o.photos.photo.length; i++) {
var photo = o.photos.photo[i];
var img = document.createElement('img');
var src = 'http://farm' + photo.farm + '.staticflickr.com/' +
photo.server + '/' + photo.id + '_' + photo.secret + '.jpg';
img.setAttribute('src', src);
// TODO: do something with the img DOM element we just created
}
这是一个live demo。
答案 1 :(得分:2)
尝试
jQuery.getJSON() - 使用GET HTTP请求从服务器加载JSON编码的数据。
或
jQuery.parseJSON - 获取格式正确的JSON字符串并返回生成的JavaScript对象