我通过jsonp获取以下对象。这是结构:
"item1": {
"child1": {
"nr": 123,
"money": "USD",
"Date": "12.12.2016, 17:00",
"asw1": 13,
"SpecialField": 33,
"another": 11,
"climbedSince": 1,
"rfeeew": 0,
"dfffe": 10
},
"child2": {
"nr": 123,
"money": "EUR",
"Date": "01.01.2017, 17:00",
"asw1": 11,
"SpecialField": 11,
"another": 11,
"climbedSince": 1,
"rfeeew": 0,
"dfffe": 10
}
},
"item2": {
"child1": {
"nr": 552,
"money": "USD",
"Date": "12.4.2016, 13:00",
"asw1": 13,
"SpecialField": 33,
"another": 44,
"climbedSince": 1,
"rfeeew": 0,
"dfffe": 10
},
"child2": {
"nr": 343,
"money": "EUR",
"Date": "01.01.2017, 17:00",
"asw1": 11,
"SpecialField": 67,
"another": 11,
"climbedSince": 1,
"rfeeew": 0,
"dfffe": 10
}
}

这是我的代码。
<script>
function getApiData(tld, callback) {
$.ajax({
url: 'https://www.website.com/api',
dataType: 'jsonp',
success: function(data) {
callback(data);
console.log(data);
},
error: function() {
console.error('Could not get lottery jackpots!');
}
});
}
function drawApiData() {
var now = new Date();
getApiData('com', function(theStuff){
$.each(theStuff, function( id, value ) {
var checkStuff=[];
if($.inArray(id, checkStuff)==-1){
checkStuff.push(id);
$('#main2').append('Field: '+id+'<br/');
}
});
});
}
drawAllData();
</script>
&#13;
我想在屏幕上用for循环打印(追加)几个值,因为对象具有尽可能多的项目(它不是静态的,但结构总是相同的)。
我可以打印item1,item2,但我无法访问第二级。
我想要打印的是例如&#34; Date&#34; +&#34; SpecialField&#34;。
提前感谢您的帮助! :)
答案 0 :(得分:0)
这就是你要找的东西:
var container = $("#main2");
var append = (obj) => {
//do something with obj object
container.append('My date: '+obj.Date);
};
for(var parent of theStuff){
for(var child of parent){
append(child);
}
}