0

Hi I am new to json/jquery. Please help.

I have a database with list of cities/states. I get it and in the html document and use json_encode to get the json in a javascript object.

var json_obj = jQuery.parseJSON('<?php echo json_encode($query); ?>');

It looks like:

"[
     {"city":"Aaronsburg","state_code":"PA"}, 
     ...
     {"city":"Abbeville","state_code":"AL"}
]"

I am trying to use the following to access each city/state:

$.each(json_obj, function() {
       $("<div>" + json_obj['state_code']+"/div>").appendTo('#test'); // I also tried json_obj.state_code
});

What I get in the output is:

undefined
...
undefined

What i need to do is actually print the city/state

Any help will be appreciated.

Thank you

4

1 回答 1

2

当前值由 jQuery 传递为:

$.each(json_obj, function(index, value) {
    $("<div>" + value.state_code + "/div>").appendTo('#test');
});

看看规格

于 2013-10-28T00:10:33.927 回答