Loading ...
Sorry, an error occurred while loading the content.

re: first time using JSON, servlet throws exception

Expand Messages
  • James
    I package up the data from a form, and send it to my servlet. This is what the servlet receives when I get the parameter. I don t know if there is a problem
    Message 1 of 7 , Nov 30, 2005
    View Source
    • 0 Attachment
      I package up the data from a form, and send it to my servlet. This is
      what the servlet receives when I get the parameter. I don't know if
      there is a problem with this file itself or what.

      {"gradedata": [{"cntr": "1","grade": "C","lastdateattended":
      ""}{"cntr": "2","grade": "C","lastdateattended": ""}{"cntr":
      "3","grade": "C","lastdateattended": ""}{"cntr": "4","grade":
      "C","lastdateattended": ""}{"cntr": "5","grade":
      "D-","lastdateattended": ""}{"cntr": "6","grade":
      "C","lastdateattended": ""}{"cntr": "7","grade":
      "C","lastdateattended": ""}{"cntr": "8","grade":
      "C","lastdateattended": ""}{"cntr": "9","grade":
      "C","lastdateattended": ""}{"cntr": "10","grade":
      "C","lastdateattended": ""}{"cntr": "11","grade":
      "C","lastdateattended": ""}{"cntr": "12","grade":
      "F","lastdateattended": "11/01/2005"}{"cntr": "13","grade":
      "C","lastdateattended": ""}{"cntr": "14","grade":
      "C","lastdateattended": ""}{"cntr": "15","grade":
      "C","lastdateattended": ""}{"cntr": "16","grade":
      "C","lastdateattended": ""}{"cntr": "17","grade":
      "C","lastdateattended": ""}{"cntr": "18","grade":
      "D","lastdateattended": ""}{"cntr": "19","grade":
      "C","lastdateattended": ""}{"cntr": "20","grade":
      "D+","lastdateattended": ""}{"cntr": "21","grade":
      "F","lastdateattended": "11/03/2005"}{"cntr": "22","grade":
      "C","lastdateattended": ""}{"cntr": "23","grade":
      "C","lastdateattended": ""}{"cntr": "24","grade":
      "C","lastdateattended": ""}{"cntr": "25","grade":
      "C","lastdateattended": ""}{"cntr": "26","grade":
      "C","lastdateattended": ""}{"cntr": "27","grade":
      "D-","lastdateattended": ""}{"cntr": "28","grade":
      "D-","lastdateattended": ""}{"cntr": "29","grade":
      "C","lastdateattended": ""}]}

      I call this function:
      JSONObject jsonobj = null;
      try {
      jsonobj = new JSONObject(grades);
      } catch (ParseException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }


      I get a syntax error in the JSONTokener class.

      I want to have an array where each item has three values.

      Thank you for your help.
    • MPCM
      Looks like you need comma between your array values (objects in your case); ... -- Matthew P. C. Morley MPCM Technologies Inc.
      Message 2 of 7 , Nov 30, 2005
      View Source
      • 0 Attachment
        Looks like you need comma between your array values (objects in your case);

        On 11/30/05, James <jblack@...> wrote:
        > I package up the data from a form, and send it to my servlet. This is
        > what the servlet receives when I get the parameter. I don't know if
        > there is a problem with this file itself or what.
        >
        > {"gradedata": [{"cntr": "1","grade": "C","lastdateattended":
        > ""}{"cntr": "2","grade": "C","lastdateattended": ""}{"cntr":
        > "3","grade": "C","lastdateattended": ""}{"cntr": "4","grade":
        > "C","lastdateattended": ""}{"cntr": "5","grade":
        > "D-","lastdateattended": ""}{"cntr": "6","grade":
        > "C","lastdateattended": ""}{"cntr": "7","grade":
        > "C","lastdateattended": ""}{"cntr": "8","grade":
        > "C","lastdateattended": ""}{"cntr": "9","grade":
        > "C","lastdateattended": ""}{"cntr": "10","grade":
        > "C","lastdateattended": ""}{"cntr": "11","grade":
        > "C","lastdateattended": ""}{"cntr": "12","grade":
        > "F","lastdateattended": "11/01/2005"}{"cntr": "13","grade":
        > "C","lastdateattended": ""}{"cntr": "14","grade":
        > "C","lastdateattended": ""}{"cntr": "15","grade":
        > "C","lastdateattended": ""}{"cntr": "16","grade":
        > "C","lastdateattended": ""}{"cntr": "17","grade":
        > "C","lastdateattended": ""}{"cntr": "18","grade":
        > "D","lastdateattended": ""}{"cntr": "19","grade":
        > "C","lastdateattended": ""}{"cntr": "20","grade":
        > "D+","lastdateattended": ""}{"cntr": "21","grade":
        > "F","lastdateattended": "11/03/2005"}{"cntr": "22","grade":
        > "C","lastdateattended": ""}{"cntr": "23","grade":
        > "C","lastdateattended": ""}{"cntr": "24","grade":
        > "C","lastdateattended": ""}{"cntr": "25","grade":
        > "C","lastdateattended": ""}{"cntr": "26","grade":
        > "C","lastdateattended": ""}{"cntr": "27","grade":
        > "D-","lastdateattended": ""}{"cntr": "28","grade":
        > "D-","lastdateattended": ""}{"cntr": "29","grade":
        > "C","lastdateattended": ""}]}
        >
        > I call this function:
        > JSONObject jsonobj = null;
        > try {
        > jsonobj = new JSONObject(grades);
        > } catch (ParseException e1) {
        > // TODO Auto-generated catch block
        > e1.printStackTrace();
        > }
        >
        >
        > I get a syntax error in the JSONTokener class.
        >
        > I want to have an array where each item has three values.
        >
        > Thank you for your help.
        >
        >
        >
        >
        >
        >
        > ________________________________
        > YAHOO! GROUPS LINKS
        >
        >
        > Visit your group "json" on the web.
        >
        > To unsubscribe from this group, send an email to:
        > json-unsubscribe@yahoogroups.com
        >
        > Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
        >
        > ________________________________
        >


        --
        Matthew P. C. Morley
        MPCM Technologies Inc.
      • James
        ... case); Thank you for the response. Now I have modified my array, and am now having a different problem. Here is my java code: temparray =
        Message 3 of 7 , Nov 30, 2005
        View Source
        • 0 Attachment
          --- In json@yahoogroups.com, MPCM <WickedLogic@g...> wrote:
          >
          > Looks like you need comma between your array values (objects in your
          case);

          Thank you for the response. Now I have modified my array, and am now
          having a different problem.

          Here is my java code:
          temparray = jsonarray.getJSONArray(t);
          System.out.println(temparray);
          cntr = temparray.getInt(0);
          grade[t] = temparray.getString(1);
          pidm[t] = bean.getBean()[cntr].getPidm();
          lastAttenddate[t] = temparray.getString(2);

          I get an error on temparray.getInt(0) with, JSONObject[0] is not a
          number.

          When I had temparray.getString(1) first the error was:
          NoSuchElementException: JSONArray[1] not found.

          temparray = [{"cntr": 1, "grade": "C", "lastdateattended": ""}]

          I don't know what I am doing wrong, now.

          Thank you for your help.
        • Martin Cooper
          ... This is an array with a single element, and that element is an object. Your original array, after you fixed the commas, was an array of objects. It looks
          Message 4 of 7 , Nov 30, 2005
          View Source
          • 0 Attachment
            On 11/30/05, James <jblack@...> wrote:
            >
            > --- In json@yahoogroups.com, MPCM <WickedLogic@g...> wrote:
            > >
            > > Looks like you need comma between your array values (objects in your
            > case);
            >
            > Thank you for the response. Now I have modified my array, and am now
            > having a different problem.
            >
            > Here is my java code:
            > temparray = jsonarray.getJSONArray(t);
            > System.out.println(temparray);
            > cntr = temparray.getInt(0);
            > grade[t] = temparray.getString(1);
            > pidm[t] = bean.getBean()[cntr].getPidm();
            > lastAttenddate[t] = temparray.getString(2);
            >
            > I get an error on temparray.getInt(0) with, JSONObject[0] is not a
            > number.
            >
            > When I had temparray.getString(1) first the error was:
            > NoSuchElementException: JSONArray[1] not found.
            >
            > temparray = [{"cntr": 1, "grade": "C", "lastdateattended": ""}]


            This is an array with a single element, and that element is an object. Your
            original array, after you fixed the commas, was an array of objects. It
            looks like you are trying to treat the objects as if they are arrays, which
            they are not.

            --
            Martin Cooper


            I don't know what I am doing wrong, now.
            >
            > Thank you for your help.
            >
            >
            >
            >
            >
            >
            >
            > Yahoo! Groups Links
            >
            >
            >
            >
            >
            >
            >
            >


            [Non-text portions of this message have been removed]
          • James Black
            ... Hash: SHA1 ... Thank you for the response. I will look at it tomorrow to see if trying to pull the values out as a JSONObject will work. If I want it to be
            Message 5 of 7 , Nov 30, 2005
            View Source
            • 0 Attachment
              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1

              Martin Cooper wrote:
              > On 11/30/05, James <jblack@...> wrote:
              >>
              >> --- In json@yahoogroups.com, MPCM <WickedLogic@g...> wrote:
              >> >
              >> > Looks like you need comma between your array values (objects in your
              >> case);
              >>
              >> Thank you for the response. Now I have modified my array, and am now
              >> having a different problem.
              >>
              >> Here is my java code:
              >> temparray = jsonarray.getJSONArray(t);
              >> System.out.println(temparray);
              >> cntr = temparray.getInt(0);
              >> grade[t] = temparray.getString(1);
              >> pidm[t] = bean.getBean()[cntr].getPidm();
              >> lastAttenddate[t] = temparray.getString(2);
              >>
              >> I get an error on temparray.getInt(0) with, JSONObject[0] is not a
              >> number.
              >>
              >> When I had temparray.getString(1) first the error was:
              >> NoSuchElementException: JSONArray[1] not found.
              >>
              >> temparray = [{"cntr": 1, "grade": "C", "lastdateattended": ""}]
              >
              >
              > This is an array with a single element, and that element is an object. Your
              > original array, after you fixed the commas, was an array of objects. It
              > looks like you are trying to treat the objects as if they are arrays, which
              > they are not.

              Thank you for the response. I will look at it tomorrow to see if
              trying to pull the values out as a JSONObject will work.

              If I want it to be an array, would I have:
              temparray = [[{"cntr": 1}],[{"grade": "C"}],[{"lastdateattended": ""}]]

              Would this be an array, where my java function would work?


              If it doesn't work then I may have to just use the String functions to
              pull out my values, on the java side; but I would prefer to use the json
              classes.

              - --
              Corruptisima republica plurimae leges. [The more corrupt a republic, the
              more laws.]
              Tacitus from Annals III, 116AD
              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.4.0 (MingW32)

              iD8DBQFDjmUsJ/zyYkX46joRAvwBAJ9YrLBMuXLzrSoLaID2X8RgKwj1TwCeNG9O
              HZc6Etv4WJtHAVlvEZ7Qrds=
              =gHss
              -----END PGP SIGNATURE-----
            • Martin Cooper
              ... Now you ve got an array where each element is an array of one element, and that element is an object. You re no better off. Fundamentally, you need to
              Message 6 of 7 , Nov 30, 2005
              View Source
              • 0 Attachment
                On 11/30/05, James Black <jblack@...> wrote:
                >
                > -----BEGIN PGP SIGNED MESSAGE-----
                > Hash: SHA1
                >
                > Martin Cooper wrote:
                > > On 11/30/05, James <jblack@...> wrote:
                > >>
                > >> --- In json@yahoogroups.com, MPCM <WickedLogic@g...> wrote:
                > >> >
                > >> > Looks like you need comma between your array values (objects in your
                > >> case);
                > >>
                > >> Thank you for the response. Now I have modified my array, and am now
                > >> having a different problem.
                > >>
                > >> Here is my java code:
                > >> temparray = jsonarray.getJSONArray(t);
                > >> System.out.println(temparray);
                > >> cntr = temparray.getInt(0);
                > >> grade[t] = temparray.getString(1);
                > >> pidm[t] = bean.getBean()[cntr].getPidm();
                > >> lastAttenddate[t] = temparray.getString(2);
                > >>
                > >> I get an error on temparray.getInt(0) with, JSONObject[0] is not a
                > >> number.
                > >>
                > >> When I had temparray.getString(1) first the error was:
                > >> NoSuchElementException: JSONArray[1] not found.
                > >>
                > >> temparray = [{"cntr": 1, "grade": "C", "lastdateattended": ""}]
                > >
                > >
                > > This is an array with a single element, and that element is an object.
                > Your
                > > original array, after you fixed the commas, was an array of objects. It
                > > looks like you are trying to treat the objects as if they are arrays,
                > which
                > > they are not.
                >
                > Thank you for the response. I will look at it tomorrow to see if
                > trying to pull the values out as a JSONObject will work.
                >
                > If I want it to be an array, would I have:
                > temparray = [[{"cntr": 1}],[{"grade": "C"}],[{"lastdateattended": ""}]]
                >
                > Would this be an array, where my java function would work?


                Now you've got an array where each element is an array of one element, and
                that element is an object. You're no better off.

                Fundamentally, you need to decide whether you want to access the fields by
                index or by name, You appear to be trying to do both, which isn't going to
                work. If you really want to use an index, then get rid of the object and
                just use an array, like so:

                temparray = [ 1, "C", "" ];

                If you want to use names (keys), get rid of the (inner) arrays and use an
                object, like so:

                tempobj = {"cntr": 1, "grade": "C", "lastdateattended": ""};

                If you do the latter, you'll need to change the Java code so that it (a)
                expects that the JSONArray contains JSONObject instances, and (b) accesses
                the fields of those objects by key instead of index.

                --
                Martin Cooper


                If it doesn't work then I may have to just use the String functions to
                > pull out my values, on the java side; but I would prefer to use the json
                > classes.
                >
                > - --
                > Corruptisima republica plurimae leges. [The more corrupt a republic, the
                > more laws.]
                > Tacitus from Annals III, 116AD
                > -----BEGIN PGP SIGNATURE-----
                > Version: GnuPG v1.4.0 (MingW32)
                >
                > iD8DBQFDjmUsJ/zyYkX46joRAvwBAJ9YrLBMuXLzrSoLaID2X8RgKwj1TwCeNG9O
                > HZc6Etv4WJtHAVlvEZ7Qrds=
                > =gHss
                > -----END PGP SIGNATURE-----
                >
                >
                >
                >
                > Yahoo! Groups Links
                >
                >
                >
                >
                >
                >
                >


                [Non-text portions of this message have been removed]
              • James
                Thank you, the servlet is now functioning properly. I decided to go with the array approach.
                Message 7 of 7 , Dec 1, 2005
                View Source
                • 0 Attachment
                  Thank you, the servlet is now functioning properly. I decided to go
                  with the array approach.
                Your message has been successfully submitted and would be delivered to recipients shortly.