JSON hash

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

    JSON hash

    Lets say we have what I would call a "hash":

    var HASH =new Array();
    HASH['one']='first';
    HASH['two']='second';
    HASH['three']='third';

    I'd like to return that as JSON data.

    Is there a direct way to do that?

    Or do I have to make it out of something like this:
    [['one'],['first']],[['two'],['second']],[['three'],['third']]

    But I can't think of a direct way of addressing that without iterating
    through the data and making a "hash".

    There must be a direct way to write this.

    Jeff
  • Peter Michaux

    #2
    Re: JSON hash

    On Mar 13, 8:43 pm, Jeff <jeff@spam_me_n ot.comwrote:
    Lets say we have what I would call a "hash":
    >
    var HASH =new Array();
    HASH['one']='first';
    HASH['two']='second';
    HASH['three']='third';
    Don't use an array for this. Use an object.

    var HASH = {};
    HASH['one']='first';
    HASH['two']='second';
    HASH['three']='third';

    I'd like to return that as JSON data.
    >
    Is there a direct way to do that?
    // http://www.json.org/json2.js
    JSON.stringify( HASH);

    Peter

    Comment

    • Stevo

      #3
      Re: JSON hash

      Peter Michaux wrote:
      On Mar 13, 8:43 pm, Jeff <jeff@spam_me_n ot.comwrote:
      >Lets say we have what I would call a "hash":
      >var HASH =new Array();
      >HASH['one']='first';
      >HASH['two']='second';
      >HASH['three']='third';
      Don't use an array for this. Use an object.
      var HASH = {};
      HASH['one']='first';
      HASH['two']='second';
      HASH['three']='third';
      I haven't had my morning coffee yet so I might be missing the point
      here, but what's wrong with doing this?

      var hash=new Object();
      hash.one='first ';
      hash.two='secon d';
      hash.three='thi rd';
      return hash;

      You're free to do this later:

      for(var i in hash)

      which will let you iterate through them all.

      Comment

      • Peter Michaux

        #4
        Re: JSON hash

        On Mar 14, 3:27 am, Stevo <ple...@spam-me.comwrote:
        Peter Michaux wrote:
        On Mar 13, 8:43 pm, Jeff <jeff@spam_me_n ot.comwrote:
        Lets say we have what I would call a "hash":
        var HASH =new Array();
        HASH['one']='first';
        HASH['two']='second';
        HASH['three']='third';
        Don't use an array for this. Use an object.
        var HASH = {};
        HASH['one']='first';
        HASH['two']='second';
        HASH['three']='third';
        >
        I haven't had my morning coffee yet so I might be missing the point
        here, but what's wrong with doing this?
        >
        var hash=new Object();
        hash.one='first ';
        hash.two='secon d';
        hash.three='thi rd';
        There is no significant difference between what you've written and
        what I wrote, as far as I know.
        return hash;
        >
        You're free to do this later:
        >
        for(var i in hash)
        >
        which will let you iterate through them all.
        The original question was about converting a "hash" to a JSON string.

        Peter

        Comment

        • Jeff

          #5
          Re: JSON hash

          Stevo wrote:
          Peter Michaux wrote:
          >On Mar 13, 8:43 pm, Jeff <jeff@spam_me_n ot.comwrote:
          >>Lets say we have what I would call a "hash":
          >>var HASH =new Array();
          >>HASH['one']='first';
          >>HASH['two']='second';
          >>HASH['three']='third';
          >Don't use an array for this. Use an object.
          >var HASH = {};
          >HASH['one']='first';
          >HASH['two']='second';
          >HASH['three']='third';
          >
          I haven't had my morning coffee yet so I might be missing the point
          here, but what's wrong with doing this?
          >
          var hash=new Object();
          hash.one='first ';
          hash.two='secon d';
          hash.three='thi rd';
          return hash;
          Well the question was in writing the "hash" server side so that
          AJAX/JSON could read it. Serializing it.

          Turns out I already knew how to do that and had written the Perl bit
          already, but Peter's reference and pointer toward ajax2.js refreshed my
          memory.

          I had forgotten how to do this:

          'hash':{'one':' first','two':'s econd','three': 'third'}

          That's JSON data, while explicitly writing out the objects and array
          is not. You get to writing in one language and forget the syntax of another.

          At any rate, Thanks, and json2.js has some interesting bits also.

          Jeff

          >
          You're free to do this later:
          >
          for(var i in hash)
          >
          which will let you iterate through them all.

          Comment

          • Peter Michaux

            #6
            Re: JSON hash

            On Mar 14, 9:04 am, Jeff <jeff@spam_me_n ot.comwrote:
            Stevo wrote:
            Peter Michaux wrote:
            On Mar 13, 8:43 pm, Jeff <jeff@spam_me_n ot.comwrote:
            >Lets say we have what I would call a "hash":
            >var HASH =new Array();
            >HASH['one']='first';
            >HASH['two']='second';
            >HASH['three']='third';
            Don't use an array for this. Use an object.
            var HASH = {};
            HASH['one']='first';
            HASH['two']='second';
            HASH['three']='third';
            >
            I haven't had my morning coffee yet so I might be missing the point
            here, but what's wrong with doing this?
            >
            var hash=new Object();
            hash.one='first ';
            hash.two='secon d';
            hash.three='thi rd';
            return hash;
            >
            Well the question was in writing the "hash" server side so that
            AJAX/JSON could read it. Serializing it.
            >
            Turns out I already knew how to do that and had written the Perl bit
            already, but Peter's reference and pointer toward ajax2.js refreshed my
            memory.
            >
            I had forgotten how to do this:
            >
            'hash':{'one':' first','two':'s econd','three': 'third'}
            >
            That's JSON data,
            As far as I know it isn't valid JSON and probably not the JavaScript
            you mean it to be either. JSON is either and Object and surrounded by
            {} or an Array and surrounded by []. The "'hash':" part you have at
            the front is outside the {} so it is not JSON.

            Peter

            Comment

            Working...