Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why does JSON suck? Example:

    Cam anyone explain why "small.jpg" is picked up, and the two remaining elements of the array are not? Thanks, in advance!

    [CODE]
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>JSON Does Not Work</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
    <script src="galleria/galleria-1.2.9.min.js"></script>
    </head>

    <body>

    <p>
    Small: <span id="jsmall"></span><br>
    Medium: <span id="jmedium"></span><br>
    Large: <span id="jlarge"></span><br>
    </p>

    <script>
    var info = {
    "images" : [
    { "sm": "small.jpg" },
    { "md": "medium.jpg" },
    { "lg": "large.jpg" }
    ]
    };

    document.getElementById( "jlarge" ).innerHTML = info.images[ 0 ].lg;
    document.getElementById( "jmedium" ).innerHTML = info.images[ 0 ].md;
    document.getElementById( "jsmall" ).innerHTML = info.images[ 0 ].sm;
    </script>

    </body>
    </html>
    [CODE]

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    info.images is an array of three elements, indexed 0,1, and 2. Each of these three elements contain an object, each with a differently named attribute.

    Code:
    document.getElementById( "jlarge" ).innerHTML = info.images[ 2 ].lg; 
    document.getElementById( "jmedium" ).innerHTML = info.images[ 1 ].md;	
    document.getElementById( "jsmall" ).innerHTML = info.images[ 0 ].sm;
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If you don't need the images array then you could use:

    Code:
    var info = {
        "images" : {
         "sm": "small.jpg" ,
         "md": "medium.jpg" ,
         "lg": "large.jpg" 
        }
    };
    then you could refer to these items as:

    Code:
    document.getElementById( "jlarge" ).innerHTML = info.images.lg; 
    document.getElementById( "jmedium" ).innerHTML = info.images.md;	
    document.getElementById( "jsmall" ).innerHTML = info.images.sm;
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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