SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: JSON/jQuery

  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    819
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    JSON/jQuery

    this is a follow-up on my previous JSON post:
    http://www.sitepoint.com/forums/show...hp?871055-JSOn

    now I'm trying to pull from this JSON with jQuery; my example is here:
    http://mayacove.com/dev/json/rockbands.html
    not working.. obviously it's more complex than the JSON in jQuery example here:

    http://api.jquery.com/jQuery.getJSON/

    how do I pull from a more complex JSON than the example @ jQuery site? I wish they had examples with more complex JSON's....

    obviously I need to be more specific about WHICH key and WHICH values, since there is more than one level..
    is there a ref. somewhere of all the methods/properties to pull from JSON's???
    (what is there apart from "key" and "val"?) are "key" and "val" JSON properties? or just plain Javascript from the array? because array is just a list, you don't have "key" and "value" in an array, right?

    also "item".. that's a jQuery JSON property? it's very vague.... an item consists of a key/value pair?

    I really need a reference for all this..

    thank you...


    (font in this editing window is still TOO SMALL, if I make it bigger, I DON'T SEE IT BIGGER, it should like in jQuery forum, you make font bigger while you're editing, you SEE it bigger...)

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    JSON just gets turned into native arrays / hashes when it's called with getJSON.
    The jquery each method is just an iterator over arrays / hashes
    Code javascript:
    [
        {
            "Name": "Beatles",
            "Country": "England",
            "YearFormed": 1959,
            "Style": "Rock'n'Roll",
            "Members": [
                "Paul",
                "John",
                "George",
                "Ringo"
            ]
        },
        {
            "Name": "Rolling Stones",
            "Country": "England",
            "YearFormed": 1959,
            "Style": "Rock'n'Roll",
            "Members": [
                "Paul",
                "John",
                "George",
                "Ringo"
            ]
        }
    ]
    Give the variables good names so you know what you are iterating over.
    Code javascript:
    $.getJSON('json/rockbands.json', function(bands) {
     
      $.each(bands, function(band) {
        console.log(band.Name)
        $.each(band.Members, function(member) {
          console.log(member);
        }
      });
     
    });

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    819
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thank you very much...

    oh brother.. I'm getting an error in CORE jQuery now... (1.4)

    Code:
    a is undefined...
    WTF????

    http://mayacove.com/dev/json/rockbands.html

    thank you...

    PS: getting same error in 1.7....

    ????


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
  •