SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery JSON Passing / Looping Issue

    Hi All,

    My ajax function passes back the following JSON STRING

    Code:
    [{"Contact":{"first_name":"Jack","last_name":"May"}},{"Contact":{"first_name":"Jane ","last_name":"May"}},{"Contact":{"first_name":"Jack","last_name":"May"}},{"Contact":{"first_name":"Margaret","last_name":"May"}}]
    How do I go about looping through the results? This is what I have but it's not working.

    Code JavaScript:
    function populateNameOptions(data)
    	{
     
    		var themessage = 'We already have the following names in the database with the same last name: ';
     
    		// LOOP THROUGH THE RESULTS
    		// ----------------------------------------------------->
    		$.each(data.results, function(i,contact){
         		// this is where we do what we want with the results
    			themessage += contact.first_name + ' ' + contact.last_name + ', ';
    		});
     
    		$('#nameNotice').html(themessage);
    	}

    Thanks

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The way your JSON array is constructed won't work with your code because each index in the array has it's own key aka Contact, see the below code which takes this into account and should work

    Code JavaScript:
    function populateNameOptions(data) {
        var themessage = 'We already have the following names in the database with the same last name: ';
     
        $.each(data.results, function(i, contact){
            themessage += contact.Contact.first_name + ' ' + contact.Contact.last_name + ', ';
        });
     
        return themessage;
    }

  3. #3
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the update. I actually figured another way of generating the json data so the first and last name were combined making the jQUERY simpler.

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    It's because your variable contact actually contains the following object:
    Code javascript:
    {"Contact":{"first_name":"Jack","last_name":"May"}}


    So to get to the first/last name you'd have to change your themessage concatenation line to:
    Code javascript:
    themessage += contact.Contact.first_name + ' ' + contact.Contact.last_name + ', ';


    The other option is of course to return your result set differently, with an array of contacts, like so:

    Code:
    [
        {"first_name": "Jack", "last_name": "May"},
        {"first_name": "Jane ", "last_name": "May"},
        {"first_name": "Jack", "last_name": "May"},
        {"first_name": "Margaret", "last_name": "May"}
    ];
    P.S. I got distracted while writing and saw Chris answered this already :|
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •