Is this JSON valid?

Discussion in 'Javascript' started by eggie5@gmail.com, Mar 10, 2007.

  1. Guest

    Is this JSON valid?

    I would like to access it like this in my javascript:

    var json=eval ('('+json+')');

    json.devices.modelNumber and json.devices.image

    Would this work?


    {
    "devices":
    [
    ["modelNumber":"VX3800", "image":"./images/VX3800.jpg"],
    ["modelNumber":"VX4566", "image":"./images/VX4566.jpg"],
    ["modelNumber":"VX4536", "image":"./images/VX4536.jpg"],
    ["modelNumber":"VX4644", "image":"./images/VX4644.jpg"],
    ["modelNumber":"VX3211", "image":"./images/VX3211.jpg"],
    ["modelNumber":"VX7857", "image":"./images/VX7857.jpg"],
    ]
    }
     
    , Mar 10, 2007
    #1
    1. Advertising

  2. Guest

    I figured it out, this is what I want:

    {
    "devices":
    [
    {"modelNumber":"VX3800", "image":"./images/VX3800.jpg"},
    {"modelNumber":"VX4566", "image":"./images/VX4566.jpg"},
    {"modelNumber":"VX4536", "image":"./images/VX4536.jpg"},
    {"modelNumber":"VX4644", "image":"./images/VX4644.jpg"},
    {"modelNumber":"VX3211", "image":"./images/VX3211.jpg"},
    {"modelNumber":"VX7857", "image":"./images/VX7857.jpg"}
    ]
    }
     
    , Mar 10, 2007
    #2
    1. Advertising

  3. RobG Guest

    On Mar 10, 3:08 pm, "" <> wrote:
    > Is this JSON valid?
    >
    > I would like to access it like this in my javascript:
    >
    > var json=eval ('('+json+')');
    >
    > json.devices.modelNumber and json.devices.image
    >
    > Would this work?


    If the JSON text itself was appropriate, yes. But your string has
    syntax errors...

    >
    > {
    > "devices":
    > [
    > ["modelNumber":"VX3800", "image":"./images/VX3800.jpg"],


    The outer Array literal is OK, but not the inner one - it should be an
    object literal. Replace the inner square brackets with curly braces:

    {"modelNumber":"VX3800", "image":"./images/VX3800.jpg"},


    > ["modelNumber":"VX4566", "image":"./images/VX4566.jpg"],
    > ["modelNumber":"VX4536", "image":"./images/VX4536.jpg"],
    > ["modelNumber":"VX4644", "image":"./images/VX4644.jpg"],
    > ["modelNumber":"VX3211", "image":"./images/VX3211.jpg"],
    > ["modelNumber":"VX7857", "image":"./images/VX7857.jpg"],


    Don't allow trailing commas in Array literals, they are handeled
    differently (and sometimes incorrectly) by different browsers, don't
    trust to chance.


    var jsonTxt = '{ "devices": ['
    + ' {"modelNumber":"VX3800", "image":"./images/VX3800.jpg"}, '
    + ' {"modelNumber":"VX4566", "image":"./images/VX4566.jpg"}, '
    + ' {"modelNumber":"VX4536", "image":"./images/VX4536.jpg"}, '
    + ' {"modelNumber":"VX4644", "image":"./images/VX4644.jpg"}, '
    + ' {"modelNumber":"VX3211", "image":"./images/VX3211.jpg"}, '
    + ' {"modelNumber":"VX7857", "image":"./images/VX7857.jpg"}'
    + ' ]'
    + '}';

    var json = eval ('('+jsonTxt+')');
    alert( 'Model: ' + json.devices[2].modelNumber // VX4536
    + '\nDevice: ' + json.devices[2].image // ./images/VX4536.jpg
    );


    --
    Rob
     
    RobG, Mar 10, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    245
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    350
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    262
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    270
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    172
    David Karr
    Jun 17, 2013
Loading...

Share This Page