2017-01-09 58 views
1

我通過jsonp獲取以下對象。這是結構:打印具有循環的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> 
 
    \t function getApiData(tld, callback) { 
 
\t $.ajax({ 
 
\t \t url: 'https://www.website.com/api', 
 
\t \t dataType: 'jsonp', 
 
\t \t success: function(data) { 
 
\t \t \t callback(data); 
 
\t \t \t console.log(data); 
 
\t \t }, 
 
\t \t error: function() { 
 
\t \t \t console.error('Could not get lottery jackpots!'); 
 
\t \t } 
 
\t }); 
 
} 
 
function drawApiData() { 
 
\t var now = new Date(); 
 
\t getApiData('com', function(theStuff){ 
 
\t \t $.each(theStuff, function(id, value) { 
 
\t \t \t var checkStuff=[]; 
 
\t \t \t if($.inArray(id, checkStuff)==-1){ 
 
\t \t \t \t checkStuff.push(id); \t 
 
\t \t \t $('#main2').append('Field: '+id+'<br/'); 
 
    \t \t \t } 
 
\t \t }); 
 
\t }); 
 
} 
 
drawAllData(); 
 
</script>

我想用打印(附加)屏幕情侶值的循環,爲許多項目的對象有(它不是靜態的,而是結構始終是相同)。

我可以打印item1,item2但我無法設法訪問第二級。

我想打印的是例如「Date」+「SpecialField」的時間。

非常感謝您的幫助! :)

+0

'$。每()'是*遍歷一個jQuery對象*不是 – RomanPerekhrest

回答

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); 
    } 
} 
+0

謝謝您的幫助有規則物體!雖然我不確定我是否理解你的循環。你會如此善意地闡述一點嗎? – Silverthan