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

Re: [json] Re: first time using JSON, servlet throws exception

Expand Messages
  • 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 1 of 7 , Nov 30, 2005
    • 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 2 of 7 , Dec 1, 2005
      • 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.