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

Re: How to parse JSON data with DataSource

Expand Messages
  • Mike K.
    Satyam, How silly of me. I was thinking in PHP terms. Thanks for your help! -Mike ... +) ) { ... 3 ... to ... so ... get 0 ... that ... what I ... the ...
    Message 1 of 7 , Sep 30, 2008
    • 0 Attachment
      Satyam,

      How silly of me. I was thinking in PHP terms. Thanks for your help!

      -Mike

      --- In ydn-javascript@yahoogroups.com, Satyam <satyam@...> wrote:
      >
      > That is correct, I often get confused about the for(... in ...), it
      > loops through the keys (not the values) to the object and, being an
      > array, it will provide a numeric key.
      >
      > This is what you want:
      >
      > for (var rate in response.results) {
      > alert(response.results[rate].jurisdictionId);
      > }
      >
      > Though this is far better:
      >
      > for (var i = 0,r = response.results, l = r.length;i<l;i+
      +) ) {
      > alert(r[i].jurisdictionId);
      > }
      >
      > Satyam
      >
      >
      > Mike K. wrote:
      > > Thanks for the tip re: YAHOO.lang.dump().
      > >
      > > alert(YAHOO.lang.dump(rate.jurisdictionId));
      > >
      > > returns undefined. I would expect that 'rate' is a JS object with
      3
      > > properties, 'jurisdictionId', 'classId', and 'rate'. But it seems
      to
      > > be a number in itself (125 for the first record, I have 126 rows,
      so
      > > I assume the array values are coming out backwards and I would
      get 0
      > > last).
      > >
      > > Since I'm getting what seems to be the array index, I'm assuming
      that
      > > my responseSchema is incorrect.
      > >
      > > What's weird is that alert(YAHOO.lang.dump(response)) returns
      what I
      > > would expect, what seems like a collection of objects.
      > >
      > > --- In ydn-javascript@yahoogroups.com, Satyam <satyam@> wrote:
      > >
      > >> I would assume that both alerts should give you "object".
      > >>
      > >> I often find that, if you don't have Firebug to simply inspect
      the
      > >> variables, using YAHOO.lang.dump() is quite useful:
      > >>
      > >> http://developer.yahoo.com/yui/docs/YAHOO.lang.html#method_dump
      > >>
      > >> Anyway, if you did:
      > >>
      > >> alert(rate.jurisdictionId);
      > >>
      > >> you should get the values for all jurisdictions.
      > >>
      > >> And regarding to DataSource suitability for this kind of data,
      > >>
      > > well, not much really, not in this case. You could simply use the
      > > Connection Manager asyncRequest and then the JSON utility parse
      > > method and you would be getting basically the same. The good
      thing
      > > about the DataSource utility is that it provides a standard
      interface
      > > to many types of data so that if it ever changes, you don't need
      to
      > > change the logic, a few properties and that's it.
      > >
      > >> Satyam
      > >>
      > >>
      > >>
      > >> Mike K. wrote:
      > >>
      > >>> --- In ydn-javascript@yahoogroups.com, Satyam <satyam@> wrote:
      > >>>
      > >>>
      > >>>> It would help if you use the same name for all, it is taxDS
      when
      > >>>>
      > >>>>
      > >>> you
      > >>>
      > >>>
      > >>>> create it, it is myDS when you set the options.
      > >>>> Also, since I see they are all numbers, you could use:
      > >>>>
      > >>>> taxDS.responseSchema.fields:
      > >>>>
      > >>>>
      > >>>
      [{key:"jurisdictionId",parser:YAHOO.util.DataSource.parseNumber},
      > >>> {key: "classId",parser:YAHOO.util.DataSource.parseNumber},
      > >>> {key:"rate",parser:YAHOO.util.DataSource.parseNumber}] };
      > >>>
      > >>>
      > >>>> Satyam
      > >>>>
      > >>>>
      > >>> Satyam,
      > >>>
      > >>> naming issue was a copy/paste error into this thread. My
      mistake.
      > >>>
      > >>> I tried what you said, and it gets me closer, but I'm getting
      the
      > >>> value of the array index for each record, and only that. I'm
      not
      > >>> getting an array of JS object, but an array of numeric values
      > >>>
      > > each
      > >
      > >>> representing an array index, but not value. Here's my code
      > >>> (unabridged):
      > >>>
      > >>> var taxDS = new YAHOO.util.DataSource("/json/
      > >>>
      > > tax_matrix.php?");
      > >
      > >>> taxDS.responseType = YAHOO.util.DataSource.TYPE_JSON;
      > >>> taxDS.responseSchema = { resultsList: "ResultSet.Matrix",
      > >>> fields: [ { key: "jurisdictionId",
      > >>> parser:YAHOO.util.DataSource.parseNumber },
      > >>> { key: "classId",
      > >>> parser:YAHOO.util.DataSource.parseNumber },
      > >>> { key: "rate",
      > >>> parser:YAHOO.util.DataSource.parseNumber } ] };
      > >>>
      > >>> var oCallback = {
      > >>> success: function(request, response, payload) {
      > >>> for (var rate in response.results) {
      > >>> alert(typeof rate);
      > >>> alert(rate);
      > >>> }
      > >>> },
      > >>> failure: function(oRequest,oResponse,oPayload) { alert
      > >>>
      > > ("bad");}
      > >
      > >>> }
      > >>> taxDS.sendRequest("", oCallback);
      > >>>
      > >>>
      > >>>
      > >>> ------------------------------------
      > >>>
      > >>> Yahoo! Groups Links
      > >>>
      > >>>
      > >>>
      > >>>
      > >>>
      > >
      ------------------------------------------------------------------------
      > >
      > >>> No virus found in this incoming message.
      > >>> Checked by AVG - http://www.avg.com
      > >>> Version: 8.0.169 / Virus Database: 270.7.5/1697 - Release Date:
      > >>>
      > > 29/09/2008 7:40
      > >
      > >>>
      > >
      > >
      > >
      > > ------------------------------------
      > >
      > > Yahoo! Groups Links
      > >
      > >
      > >
      > >
      ------------------------------------------------------------------------
      > >
      > >
      > > No virus found in this incoming message.
      > > Checked by AVG - http://www.avg.com
      > > Version: 8.0.169 / Virus Database: 270.7.5/1697 - Release Date:
      29/09/2008 7:40
      > >
      > >
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.