What is wrong with my JSON string???

Discussion in 'Javascript' started by pbd22, Feb 6, 2008.

  1. pbd22

    pbd22 Guest

    Hi.

    I can't seem to get any values from the below string.
    I have tried myJSONObject.slides[0], myJSONObject.slides['1'],
    myJSONObject.slides[0].1, etc.
    I can't get it to work. Is it because the NAMES are numbers? Can you
    get this to work?

    Thanks.

    "{'slides': [{'2': 's002.jpg', '3': 's003.jpg', '4': 's004.jpg', '5':
    's005.jpg', '6': 's006.jpg', '7': 's007.jpg', '48': 's048.jpg', '17':
    's017.jpg', '27': 's027.jpg', '37': 's037.jpg', '15': 's015.jpg',
    '25': 's025.jpg', '35': 's035.jpg', '13': 's013.jpg', '23':
    's023.jpg', '33': 's033.jpg', '11': 's011.jpg', '21': 's021.jpg',
    '31': 's031.jpg', '41': 's041.jpg', '47': 's047.jpg', '57':
    's057.jpg', '19': 's019.jpg', '29': 's029.jpg', '39': 's039.jpg',
    '46': 's046.jpg', '56': 's056.jpg', '49': 's049.jpg', '51':
    's051.jpg', '42': 's042.jpg', '52': 's052.jpg', '45': 's045.jpg',
    '55': 's055.jpg', '16': 's016.jpg', '26': 's026.jpg', '36':
    's036.jpg', '14': 's014.jpg', '24': 's024.jpg', '34': 's034.jpg',
    '12': 's012.jpg', '22': 's022.jpg', '32': 's032.jpg', '10':
    's010.jpg', '20': 's020.jpg', '30': 's030.jpg', '40': 's040.jpg',
    '50': 's050.jpg', '44': 's044.jpg', '54': 's054.jpg', '18':
    's018.jpg', '28': 's028.jpg', '38': 's038.jpg', '8': 's008.jpg', '9':
    's009.jpg', '43': 's043.jpg', '53': 's053.jpg', '1': 's001.jpg'}]};"
    pbd22, Feb 6, 2008
    #1
    1. Advertising

  2. pbd22

    GArlington Guest

    On Feb 6, 1:41 pm, pbd22 <> wrote:
    > Hi.
    >
    > I can't seem to get any values from the below string.
    > I have tried myJSONObject.slides[0], myJSONObject.slides['1'],
    > myJSONObject.slides[0].1, etc.
    > I can't get it to work. Is it because the NAMES are numbers? Can you
    > get this to work?
    >
    > Thanks.
    >
    > "{'slides': [{'2': 's002.jpg', '3': 's003.jpg', '4': 's004.jpg', '5':
    > 's005.jpg', '6': 's006.jpg', '7': 's007.jpg', '48': 's048.jpg', '17':
    > 's017.jpg', '27': 's027.jpg', '37': 's037.jpg', '15': 's015.jpg',
    > '25': 's025.jpg', '35': 's035.jpg', '13': 's013.jpg', '23':
    > 's023.jpg', '33': 's033.jpg', '11': 's011.jpg', '21': 's021.jpg',
    > '31': 's031.jpg', '41': 's041.jpg', '47': 's047.jpg', '57':
    > 's057.jpg', '19': 's019.jpg', '29': 's029.jpg', '39': 's039.jpg',
    > '46': 's046.jpg', '56': 's056.jpg', '49': 's049.jpg', '51':
    > 's051.jpg', '42': 's042.jpg', '52': 's052.jpg', '45': 's045.jpg',
    > '55': 's055.jpg', '16': 's016.jpg', '26': 's026.jpg', '36':
    > 's036.jpg', '14': 's014.jpg', '24': 's024.jpg', '34': 's034.jpg',
    > '12': 's012.jpg', '22': 's022.jpg', '32': 's032.jpg', '10':
    > 's010.jpg', '20': 's020.jpg', '30': 's030.jpg', '40': 's040.jpg',
    > '50': 's050.jpg', '44': 's044.jpg', '54': 's054.jpg', '18':
    > 's018.jpg', '28': 's028.jpg', '38': 's038.jpg', '8': 's008.jpg', '9':
    > 's009.jpg', '43': 's043.jpg', '53': 's053.jpg', '1': 's001.jpg'}]};"


    Did you try myJSONObject.slides[0]['2']?
    GArlington, Feb 6, 2008
    #2
    1. Advertising

  3. pbd22

    pbd22 Guest

    yes, thanks.

    I get slides.0 is null or not an object.

    should I be using eval?

    when I try

    var x = xmlhttp.responseText;
    var myJSONObject = eval("(" + x + ")");

    I get "(" expected.

    When I try

    var x = xmlhttp.responseText;
    var myJSONObject = eval(x);

    I get ";" expected;

    I am lost. Why I am I getting these errors?
    try/catch isn't really helping. Would somebody
    mind throwing my string in their compiler to see
    if you get different results?

    thanks.
    pbd22, Feb 6, 2008
    #3
  4. pbd22

    Henry Guest

    On Feb 6, 1:41 pm, pbd22 <> wrote:
    <snip>
    > "{'slides': [{'2': 's002.jpg', '3': 's003.jpg', '4': 's004.jpg', '5':

    <snip>
    > 's009.jpg', '43': 's043.jpg', '53': 's053.jpg', '1': 's001.jpg'}]};"

    ^

    That semicolon at the end of your "JSON" string is erroneous.
    Henry, Feb 6, 2008
    #4
  5. pbd22

    GArlington Guest

    On Feb 6, 3:35 pm, pbd22 <> wrote:
    > yes, thanks.
    >
    > I get slides.0 is null or not an object.
    >
    > should I be using eval?
    >
    > when I try
    >
    > var x = xmlhttp.responseText;
    > var myJSONObject = eval("(" + x + ")");
    >
    > I get "(" expected.
    >
    > When I try
    >
    > var x = xmlhttp.responseText;
    > var myJSONObject = eval(x);
    >
    > I get ";" expected;
    >
    > I am lost. Why I am I getting these errors?
    > try/catch isn't really helping. Would somebody
    > mind throwing my string in their compiler to see
    > if you get different results?
    >
    > thanks.


    I tried it this way (NOT using JSON, just plain javascript)

    function setVars(){
    thisVal = {'slides': [{'2': 's002.jpg', '3': 's003.jpg', '4':
    's004.jpg', '5':'s005.jpg', '6': 's006.jpg', '7': 's007.jpg', '48':
    's048.jpg', '17':'s017.jpg', '27': 's027.jpg', '37': 's037.jpg', '15':
    's015.jpg','25': 's025.jpg', '35': 's035.jpg', '13': 's013.jpg',
    '23':'s023.jpg', '33': 's033.jpg', '11': 's011.jpg', '21':
    's021.jpg','31': 's031.jpg', '41': 's041.jpg', '47': 's047.jpg',
    '57':'s057.jpg', '19': 's019.jpg', '29': 's029.jpg', '39':
    's039.jpg','46': 's046.jpg', '56': 's056.jpg', '49': 's049.jpg',
    '51':'s051.jpg', '42': 's042.jpg', '52': 's052.jpg', '45':
    's045.jpg','55': 's055.jpg', '16': 's016.jpg', '26': 's026.jpg',
    '36':'s036.jpg', '14': 's014.jpg', '24': 's024.jpg', '34':
    's034.jpg','12': 's012.jpg', '22': 's022.jpg', '32': 's032.jpg',
    '10':'s010.jpg', '20': 's020.jpg', '30': 's030.jpg', '40':
    's040.jpg','50': 's050.jpg', '44': 's044.jpg', '54': 's054.jpg',
    '18':'s018.jpg', '28': 's028.jpg', '38': 's038.jpg', '8': 's008.jpg',
    '9':'s009.jpg', '43': 's043.jpg', '53': 's053.jpg', '1':
    's001.jpg'}]};
    }
    function getVars(){
    alert(thisVal.slides[0]['2']);
    }

    and it worked...
    What was your problem?
    GArlington, Feb 6, 2008
    #5
  6. pbd22

    pbd22 Guest

    Thanks both.

    Henry was right, the semicoln shouldn't have been there.

    Thanks again.
    Peter
    pbd22, Feb 7, 2008
    #6
  7. pbd22 wrote:
    > Thanks both.
    >
    > Henry was right, the semicoln shouldn't have been there.


    JSLint can find these sorts of problems in JSON text.

    http://www.JSLint.com/
    Douglas Crockford, Feb 8, 2008
    #7
    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:
    231
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    317
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    257
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    155
    David Karr
    Jun 17, 2013
Loading...

Share This Page