By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,933 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,933 IT Pros & Developers. It's quick & easy.

JSON hash

P: n/a
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
Mar 14 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mar 13, 8:43 pm, Jeff <jeff@spam_me_not.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
Mar 14 '08 #2

P: n/a
Peter Michaux wrote:
On Mar 13, 8:43 pm, Jeff <jeff@spam_me_not.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='second';
hash.three='third';
return hash;

You're free to do this later:

for(var i in hash)

which will let you iterate through them all.
Mar 14 '08 #3

P: n/a
On Mar 14, 3:27 am, Stevo <ple...@spam-me.comwrote:
Peter Michaux wrote:
On Mar 13, 8:43 pm, Jeff <jeff@spam_me_not.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='second';
hash.three='third';
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
Mar 14 '08 #4

P: n/a
Stevo wrote:
Peter Michaux wrote:
>On Mar 13, 8:43 pm, Jeff <jeff@spam_me_not.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='second';
hash.three='third';
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':'second','three':'thir d'}

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.
Mar 14 '08 #5

P: n/a
On Mar 14, 9:04 am, Jeff <jeff@spam_me_not.comwrote:
Stevo wrote:
Peter Michaux wrote:
On Mar 13, 8:43 pm, Jeff <jeff@spam_me_not.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='second';
hash.three='third';
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':'second','three':'thir d'}

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
Mar 14 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.