Here's what I use in a similar application:
PHP:
header("Content-Type: application/json");
if ( $_REQUEST["jsoncallback"] ) {
$callback = htmlentities( $_REQUEST["jsoncallback"] );
}
$output = json_encode( $array );
if ( $callback ) {
$output = $callback . "(" . $output . ")"; // I know the variables can be embedded in the strings, I don't care, I like it this way.
}
echo $output;
Javascript:
var getURL = "http://www.example.com/script.php?jsoncallback=?";
jQuery.ajax({
dataType: "json",
url: getURL,
success: function(data){
var obj = jQuery.parseJSON( data );
// now you can loop through the data object
}
});
For the looping part, this question/answer might be helpful: How to Loop through plain JavaScript object with objects as members?