JQuery, looping through JSON fails
I've read the other almost exact questions but am still failing miserably. I give. I've tried every answer here ....
Here's a sample of the json:
[{"count":4,"date":"2010-03-02 09:30:00","id":2开发者_高级运维91,"code":"BPT"},
{"count":4,"date":"2010-03-02 10:30:00","id":292,"code":"BPT"},
{"count":3,"date":"2010-03-02 11:30:00","id":293,"code":"BPT"},
{"count":9,"date":"2010-03-02 12:30:00","id":294,"code":"BPT"},
{"count":8,"date":"2010-03-02 13:30:00","id":295,"code":"BPT"},
{"count":8,"date":"2010-03-02 14:30:00","id":296,"code":"BPT"},
{"count":17,"date":"2010-03-02 15:30:00","id":297,"code":"BPT"}]
Here's the current blind stab at getting this to loop through each hash in the array:
<body>
<div id="junk">
</div>
<script type='text/javascript'>
$.getJSON('bpt.json', function(data) {
$.each(data, function() {
$("#junk").html(this.count + "<br />");
});
});
</script>
</body>
I'm using firebug and I'm not sure where to look. It only prints the first value or key. Firebug does show that the json is getting parsed correctly.
do you want to print each 'count'? if so, you'll have to use append() instead of html(). html() replaces the entire innerHtml.
$("#junk").append(this.count + "<br />");
Example: http://www.jsfiddle.net/Fndmp/
This works.
$("#junk").append(data[i].date + "<br />");
This doesn't.
$("#junk").html(this.count + "<br />");
I apologize if this was obvious to some of you. First time JQuery guest, long time listener.
精彩评论