SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: JSOn

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

    JSOn

    man.. I hate JSON, it's so hard.. something like this is simple...
    Code:
    {
        "Name": "Beatles",
        "Country": "England",
        "YearFormed": 1959,
        "Style": "Rock'n'Roll",
        "Members": [
            "Paul",
            "John",
            "George",
            "Ringo"
        ]
    }

    but how do I add a second band?


    if I add a comma at the end, and repeat, with a diff band name, it doesn't validate in JSON Lint, i.e., this doesn't validate...

    Code:
    {
        "Name": "Beatles",
        "Country": "England",
        "YearFormed": 1959,
        "Style": "Rock'n'Roll",
        "Members": [
            "Paul",
            "John",
            "George",
            "Ringo"
        ]
    },
    {
        "Name": "Rolling Stones",
        "Country": "England",
        "YearFormed": 1956,
        "Style": "Rock'n'Roll",
        "Members": [
            "Mick",
            "Keith",
            "Brian",
            "Charlie"
        ]
    }

    all the JSON tutorials have really simple examples, but when you get to the real world JSON's can be very complex...

    I need examples of more complex JSON examples to practice with...

    thank you..

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your JSON is actually valid, the JSON linter I find to be a bit glitchy as it expects a very specific array to be passed to it but in saying that you shouldn't have any problem with the above if you wrap it with []

    Code JavaScript:
    [{
        "Name": "Beatles"
    }, {
        "Name": "Rolling Stones"
    }]
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

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

    ok, so you can wrap a JSON in either {...} or [....]

    where can I find some literature on this??

    JSON syntax is complex.. I need documentation...

    (and is there a more reliable validator, then, than Lint?)

    thank you...

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No you can only use the square brackets as its a collection of values, trying to wrap a JSON object using curly brackets would cause a syntax error as objects require a key to be associated with each individual array index.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle


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
  •