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

1 Answer 1

2

The curreent value is passed by jQuery as:

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

Take a look at the specs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.