0% found this document useful (0 votes)
709 views5 pages

Jquery Loop On Json Data Using $.each - Stack Overflow - EfrainBonilla

This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. Jquery loop on json data using $.each but I am running into problems where the alert is showing undefined.

Uploaded by

Efrain Bonilla
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
709 views5 pages

Jquery Loop On Json Data Using $.each - Stack Overflow - EfrainBonilla

This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. Jquery loop on json data using $.each but I am running into problems where the alert is showing undefined.

Uploaded by

Efrain Bonilla
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Welcome to Q&A for professional and enthusiast programmers check out the FAQ!

log in | careers | chat | meta | about | faq search

Questions

Tags

Users

Badges

Unanswered

Ask Question

jquery loop on Json data using $.each


Hello World!
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. about faq
[ {"Id": 10004, "PageName": "club"}, {"Id": 10040, "PageName": "qaz"}, {"Id": 10059, "PageName": "jjjjjjj"} ] and I am trying to loop through the collection using $.each but I am running into problems where the alert is showing undefined. I have tried alot of different syntax but can't seem to figure this out. The JQuery I am using is $.each(data, function(i, item) { alert(item.PageName); }); Can any one point me in the right direction? EDIT This is the code I am using to grab the data $.getJSON('/Cms/GetPages/123', null, function(data) { fillSelect(data); }); and this is the function that gets called upon call back function fillSelect(data) { alert(data); $.each(data, function(i, item) { alert(item.PageName); }); } EDIT 2 This is slightly confusing me, according to the docs it should work as I have it, but it doesn't. According to fiddler the header shows:Content-Type: application/json; charset=utf-8 and the JSON is exactly correct above. I am using chrome if this makes any different. Will test in IE and FF.... EDIT 3 using $.get produces

Hi I have the following JSON returned in a variable called data.

9
6

THIS IS THE JSON THAT GETS RETURNED...

tagged
jquery json

132025 16406 1891

foreach

asked 1 year ago viewed 14,114 times active 1 year ago

Programmable Programmable logic and FPGA logic and FPGA design Q&A design Q&A
57% committed

DEVELOPER @Hyves; social networking company in Amsterdam, Hyves Amsterdam, Netherlands Software Developer - Good in Perl or willing to Learn? Booking.com Amsterdam, Netherlands Vliegende software developer Anno MMX BV Amsterdam, Netherlands

"[\r\n {\r\n \"Id\": 10041,\r\n \"PageName\": \"01234567890\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10001,

Linked

jquery

json

foreach

How to get first member of an object in javascript

link | improve this question

edited Feb 26 '10 at 15:55

asked Feb 26 '10 at 14:47 Rippo 4,463 2 11 31 98% accept rate

How to loop through JSON object together with an if-statement?

Related
jQuery - $.each loop through json code jQuery append child nodes in for each Jquery Each Json Object Using Jquery each loop to display six list items per ul inside a div pulled from a JSON object

It works for me. Make sure data are passed correctly to each method. kgiannakakis Feb 26 '10 at 14:51

feedback

3 Answers

active

oldest

votes

Jquery each loop with json array

jquery each on a JSON api

10

var data = [ {"Id": 10004, "PageName": "club"}, {"Id": 10040, "PageName": "qaz"}, {"Id": 10059, "PageName": "jjjjjjj"} ]; $.each(data, function(i, item) { alert(data[i].PageName); }); $.each(data, function(i, item) { alert(item.PageName); }); these two options work well, unless you have something like: var data.result = [ {"Id": 10004, "PageName": "club"}, {"Id": 10040, "PageName": "qaz"}, {"Id": 10059, "PageName": "jjjjjjj"} ]; $.each(data.result, function(i, item) { alert(data.result[i].PageName); }); EDIT: try with this and describes what the result $.get('/Cms/GetPages/123', function(data) { alert(data); }); FOR EDIT 3: this corrects the problem, but not the idea to use "eval", you should see how are the response in '/Cms/GetPages/123'. $.get('/Cms/GetPages/123', function(data) { $.each(eval(data.replace(/[\r\n]/, "")), function(i, item) { alert(item.PageName); }); });
link | improve this answer edited Feb 26 '10 at 16:32 answered Feb 26 '10 at 14:55 andres descalzo 3,829 1 6 22

loop through returned json using jquery Filter json data using jquery? Using PHP's foreach to create array of values from JSON data Pausing execution in jQuery $.each loop for JSON items Parsing the JSON data using jquery? What is the equivalent of a for-each loop in jQuery? JQuery using each() as classic for each loop and not all at once How to pass a javascript variable into an Each loop (jQuery) on an JSON object jquery loop through json How to add json data into select tag in jquery Create one list item for each year using JQuery applying loop to a data retrieved as json jquery fill dropdown with json data Help with jQuery $.each loop and json data How to loop through this JSON Data using Jquery to display klout score? JSON Loop using jquery iterating returned JSON OBJECT using jQUERY $.each() display item counts per month using JSON jquery each() IF statement (for loop?) jquery each and a JSON string

Looks like I needed to add eval(data) Rippo Feb 26 '10 at 15:01 in the function "httpData" in jQuery you can see a called window["eval"] for the json data. you do not need to use eval. this line "$. get ( '/ Cms/GetPages/123'" is to show you that are receiving in "data". andres descalzo Feb 26 '10 at 15:40 james.padolsey.com/jquery/#v=1.3.2&fn=jQuery.ajax james.padolsey.com/jquery/#v=1.3.2&fn=jQuery.httpData andres descalzo Feb 26 '10 at 15:41 Have updated answer (see EDIT 3) to show what $.get produces Rippo Feb 26 '10 at 16:02 how are you returning the data in "/ Cms/GetPages/123"? andres descalzo Feb 26 '10 at 16:23 show 1 more comment feedback

Have you converted your data from string to JavaScript object?

You can do it with data = eval('(' + string_data + ')'); or, which is safer, data = JSON.parse(string_data); but later will only works in FF 3.5 or if you include json2.js jQuery since 1.4.1 also have function for that, $.parseJSON() . But actually, $.getJSON() should give you already parsed json object, so you should just check everything thoroughly, there is little mistake buried somewhere, like you might have forgotten to quote something in json, or one of the brackets is missing.

link | improve this answer

edited Feb 26 '10 at 15:40

answered Feb 26 '10 at 14:57 vava 6,546 17 44

Looks like I need to add fillselect(eval(data)); Rippo Feb 26 '10 at 15:00 BTW how compatible is eval ? Rippo Feb 26 '10 at 15:01

That should be data = eval('('+string_data+')'); . Also, jQuery has another function data = jQuery.parseJSON(string_data); Greg Feb 26 '10 at 15:11 There is something not quite right. only eval(data) works. Please see my EDIT 2 Rippo Feb 26 '10 at 15:22 @Greg, thanks, updated answer vava Feb 26 '10 at 15:41

feedback

getJSON will evaluate the data to JSON for you, as long as the correct content-type is used. Make sure that the server is returning the data as application/json.
link | improve this answer answered Feb 26 '10 at 15:05 kgiannakakis 42.5k 3 43 79

according to fiddler the content type is Content-Type: application/json; charset=utf-8 Rippo Feb 26 '10 at 15:23

feedback

Your Answer

Name Email
or

log in

Home Page

Post Your Answer

Not the answer you're looking for? Browse other questions tagged
jquery json foreach or ask your own question.

question feed

about | faq |

new

blog | chat | data | podcast | shop | legal | advertising info | mobile | contact us | feedback

stackoverflow.com api/apps careers serverfault.com superuser.com meta area 51 webapps gaming ubuntu webmasters cooking game development math photography stats tex english theoretical cs programmers unix apple wordpress physics home improvement gis electronics android security bicycles dba drupal sharepoint
site design / logo 2011 stack exchange inc; user contributions licensed under cc-wiki with attribution required

rev 2011.12.1.1

P geSna a p.net
http://www.pagesnap.net/?locale=en

BT EA
Generate PDF!

change any website into PDF document

You might also like