我正在尝试使用专用函数在 json 中获取特定元素,但我无法弄清楚它为什么不起作用。谁能帮我?
我在这里设置了一个小提琴:http: //jsfiddle.net/jonigiuro/N5TTM/2/
代码:
var data = {
"results":[
{
"reinsurance":{
"africa":{
"one":{
"name":"test",
"address":"test street",
"tel1":"tel1",
"tel2":"tel2",
"url":"http://www.google.com"
}
}
}
}
]
}
// FAKE RESPONSE
var AjaxInfo = {
init : function() {
this.bindEvents();
},
bindEvents : function() {
var self = this;
$(document).ready(function() {
self.renderData( 'reinsurance', 'africa', 'one' );
})
},
renderData : function( category, country, name ) {
console.log(data.results[0]); //ok
console.log(data.results[0].category); //I pass 'insurance' as a category but it returns "undefined" because it looks for "category" instead
}
}
AjaxInfo.init();