How to iterate over json array in javascript

Viewed 24872

Yes there are many post regarding this.But my doubt is little different.I have following array for example

var dictionary = {
    "12Jan2013": [{
        "id": "0",
        "name": "ABC"
    }, {
        "id": "1",
        "name": "DEF"
    }],
    "13Jan2013": [{
        "id": "0",
        "name": "PQR"
    }, {
        "id": "1",
        "name": "xyz"
    }]
};

Same post is there on same site BUT here in dictionary json array key is dynamic.Here it is date ie 12Jan2013.It can be any date.It is not static.I have searched for that but didn't get solution.

How to iterate over such a json array?

AND How to print json array as in same formate shown above?

EDIT

Here is my real code.And i shown a comment in following code where i wanted to iterate data ie jsonData var in getWeatherDataForCities callback

var arrAllrecords = [];
var arrCityrecordForADay = [];
function getWeatherDataForCities(cityArray, callback){

var toDaysTimestamp = Math.round((new Date()).getTime() / 1000) - (24*60*60);
for(var i in cityArray){

    for(var j=1; j<=1; j++){
        var jsonurl = "http://api.openweathermap.org/data/2.5/history/city?q="+cityArray[i]+"&dt="+toDaysTimestamp;

        $.ajax({
            url: jsonurl,
            dataType: "jsonp",
            mimeType: "textPlain",
            crossDomain: true,
            contentType: "application/json; charset=utf-8",
            success: function(data){                    
                var arrCityRecordForDay = [];
                /*arrCityrecordForADay.push(data.list[0].city.name);
                arrCityrecordForADay.push(data.list[0].weather[0].description);
                arrCityrecordForADay.push(timeConverter(data.list[0].dt));
                arrCityrecordForADay.push(data.list[0].main.temp);
                arrCityrecordForADay.push(data.list[0].main.humidity);
                arrCityrecordForADay.push(data.list[0].main.pressure)
                arrCityrecordForADay.push(data.list[0].wind.speed);*/
                //'{"pets":[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';

                arrCityRecordForDay.push(
                    {"cityName" : data.list[0].city.name},
                    {"weather" : data.list[0].weather[0].description}
                );

                var tempId = data.list[0].city.name+"-"+timeConverter(data.list[0].dt);

                arrCityrecordForADay.push(
                    {tempId : arrCityRecordForDay}
                );

                if(((arrCityrecordForADay.length)) === cityArray.length) {
                    callback(arrCityrecordForADay);
                }

        } });
        toDaysTimestamp = toDaysTimestamp - (24*60*60);
    }   
}       
}

$(document ).ready(function() {

 var cityArray = new Array();
  cityArray[0] = "pune";

  getWeatherDataForCities(cityArray, function(jsonData) {
        // Here I want to iterate jsonData
  });


});
4 Answers
Related