jQuery的 - 解析JSON数据 - 经与变量名的麻烦 [英] jQuery - parsing JSON data - Having trouble with variable name
问题描述
我的第一个钻研与JSON数据的工作。我有一点使用jQuery经验虽然。
My first delve into working with JSON data. I have a bit of experience using jQuery though.
我张贴此URL(tumblr API):jyoseph.com/api/read/json
I'm posting to this URL (tumblr api): jyoseph.com/api/read/json
我试图做的是输出被返回的JSON。我有什么至今:
What I'm trying to do is output the json that gets returned. What I have so far:
$(document).ready(function(){
$.getJSON("http://jyoseph.com/api/read/json?callback=?",
function(data) {
//console.log(data);
console.log(data.posts);
$.each(data.posts, function(i,posts){
var id = this.id;
var type = this.type;
var date = this.date;
var url = this.url;
var photo500 = this.photo-url-500;
$('ul').append('<li> ' +id+ ' - ' +type+ ' - ' +date+ ' - ' +url+ ' - ' +photo500+ ' - ' + ' </li>');
});
});
});
请参阅我的jsbin后整个脚本: http://jsbin.com/utaju/edit
See my jsbin post for the entire script: http://jsbin.com/utaju/edit
一些来自tumblr键有 - 连字符在其中,这似乎引起问题。正如你所看到的照片网址-500或其他照片说明是导致脚本突破,它的输出为NaN。
Some of the keys from tumblr have "-" hyphens in them, and that seem to be causing a problem. As you can see "photo-url-500" or another "photo-caption" is causing the script to break, it's outputting NaN.
是否有与键名有连字符的问题吗?还是我要对此都错了?
Is there a problem with having hyphens in the key names? Or am I going about this all wrong?
推荐答案
如果有在名称破折号,你需要以不同的方式访问它们。更改 VAR photo500 = this.photo-URL-500;
读 VAR photo500 =本[照片网址-500];
。
If there are dashes in the names you'll need to access them differently. Change var photo500 = this.photo-url-500;
to read var photo500 = this["photo-url-500"];
.
这篇关于jQuery的 - 解析JSON数据 - 经与变量名的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!