What is wrong with my JSON string???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pbd22

    What is wrong with my JSON string???

    Hi.

    I can't seem to get any values from the below string.
    I have tried myJSONObject.sl ides[0], myJSONObject.sl ides['1'],
    myJSONObject.sl ides[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'}]};"
  • GArlington

    #2
    Re: What is wrong with my JSON string???

    On Feb 6, 1:41 pm, pbd22 <dush...@gmail. comwrote:
    Hi.
    >
    I can't seem to get any values from the below string.
    I have tried myJSONObject.sl ides[0], myJSONObject.sl ides['1'],
    myJSONObject.sl ides[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.sl ides[0]['2']?

    Comment

    • pbd22

      #3
      Re: What is wrong with my JSON string???

      yes, thanks.

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

      should I be using eval?

      when I try

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

      I get "(" expected.

      When I try

      var x = xmlhttp.respons eText;
      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.

      Comment

      • Henry

        #4
        Re: What is wrong with my JSON string???

        On Feb 6, 1:41 pm, pbd22 <dush...@gmail. comwrote:
        <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.

        Comment

        • GArlington

          #5
          Re: What is wrong with my JSON string???

          On Feb 6, 3:35 pm, pbd22 <dush...@gmail. comwrote:
          yes, thanks.
          >
          I get slides.0 is null or not an object.
          >
          should I be using eval?
          >
          when I try
          >
          var x = xmlhttp.respons eText;
          var myJSONObject = eval("(" + x + ")");
          >
          I get "(" expected.
          >
          When I try
          >
          var x = xmlhttp.respons eText;
          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.s lides[0]['2']);
          }

          and it worked...
          What was your problem?

          Comment

          • pbd22

            #6
            Re: What is wrong with my JSON string???

            Thanks both.

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

            Thanks again.
            Peter

            Comment

            • Douglas Crockford

              #7
              Re: What is wrong with my JSON string???

              pbd22 wrote:
              Thanks both.
              >
              Henry was right, the semicoln shouldn't have been there.
              JSLint can find these sorts of problems in JSON text.

              JSLint, The JavaScript Code Quality and Coverage Tool. This file allows JSLint to be run from a web browser. It can accept a source program and analyze it without sending it over the network.

              Comment

              Working...