I need to iterate through a JSON object in JavaScript or jQuery that is returned from a PHP file with the a json_encode function call:

echo json_encode ($varDump);

The beginning of the JSON objects looks like the following:

["date","Rescription",{"date":"1715-05-03","description":"Edmund Halley observes total eclipse phenomenon \"Baily's Beads\""},{"date":"1765-05-03","description":"1st US Medical school opened"},{"date":"1797-05-03","description":"Jamaica discovered by Columbus"},{"date":"1802-05-03","description":"Washington, D.C., was incorporated as a city."},{"date":"1830-05-03","description":"1st regular steam train passenger service starts"},

I need to pull out the date and the description.
I tried the following but I am getting errors:

            count = data.length - 1;
            for(i=0; i < count; i++){
                $('#currentText').html(data.date[i]);
                $('#currentText').html(data.description[i]);
            }    

The value in count on my test case seems way high, as if it’s counting characters and FF says data.date is not an object.
How would I go about doing this?

I made some progress and found the correct syntax for displaying the array, but I can't seem to output individual data elements.
For testing I wrote this function:

success: function(data){
                $('#currentText').append(data);
                
                count = data.length;
                for(i=2; i < count; i++){
                    $('#currentText').append(data[i].date);
                    $('#currentText').append(data[i].description);
                }
            }, //end success function

On my web page I see the first append as I expcted, the whole data structure.
For the second and third append, nothing appears

Any thoughts?

I now have a JSON file that looks like this:

["date","Rescription",{"date":"1715-05-03","description":"Edmund Halley observes total eclipse phenomenon \"Baily's Beads\""},{"date":"1765-05-03","description":"1st US Medical school opened"},{"date":"1797-05-03","description":"Jamaica discovered by Columbus"},

But the key seems misspelled for description, and statements like

console.log(data[4].date);
console.log(data[4].description);

produce an undefined error.

Found the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.