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

637RE: [json] json as js variable

Expand Messages
  • Atif Aziz
    Dec 19 5:30 AM
    • 0 Attachment
      I think I know what you're looking for here. Suppose the following

      var table = {
      "columns" : [ "FirstName", "LastName" ],
      "rows" : [
      [ "Mickey", "Mouse" ],
      [ "Donald", "Duck" ],
      [ "Minnie", "Mouse" ]

      Suppose further a function we add to the Array object's prototype:

      Array.prototype.toObject = function(names)
      var o = {};
      for (var i = 0; i < names.length; i++)
      o[names[i]] = i < this.length ? this[i] : null;

      return o;

      Now you can conveniently convert any row of the table into an object in
      one swell swoop like this:

      var o = table.rows[1].toObject(table.columns);

      This gives you non-repeating column names in the original table
      definition in JSON, which may be useful to keep the data on the wire
      small, yet object-like access (dot-syntax) to a row once in script.

      Hope this help.

      -----Original Message-----
      From: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of
      Mark Ireland
      Sent: Sunday, December 17, 2006 11:19 PM
      To: json@yahoogroups.com
      Subject: Re: [json] json as js variable

      Thanks, that got it working.

      Now I am wondering if there is some alternative to repeating the column
      names in every object.

      >From: "Matthew Morley" <WickedLogic@...
      <mailto:WickedLogic%40gmail.com> >
      >Reply-To: json@yahoogroups.com <mailto:json%40yahoogroups.com>
      >To: json@yahoogroups.com <mailto:json%40yahoogroups.com>
      >Subject: Re: [json] json as js variable
      >Date: Mon, 11 Dec 2006 06:41:03 -0500
      >*Identifiers* are names for constants, variables, functions, loop
      >objects and classes. The first character must be an ASCII letter,
      >or dollar sign. Following characters can also include digits.
      >Even 'u80020' will do. You can only use numeric indexes if you are
      >an array.
      >Matthew P. C. Morley
      >[Non-text portions of this message have been removed]

      Join the millions of Australians using Live Search. Try live.com.au
    • Show all 19 messages in this topic