Jquery Loop On Json Data Using $.each - Stack Overflow - EfrainBonilla
Jquery Loop On Json Data Using $.each - Stack Overflow - EfrainBonilla
Questions
Tags
Users
Badges
Unanswered
Ask Question
9
6
tagged
jquery json
foreach
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
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
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
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.
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
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!