473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Mar 14 '08 #1
5 8142
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff Thies | last post by:
I have this: var hash=new Array(); hash='some_value'; hash='some other value'; Whar do you call that in javascript? I know it's possible to define this in one line, but I've forgotten how....
2
by: kungfumike | last post by:
I am currently having an issue getting json to work. The version of json,js I am using can be found here: http://www.json.org/json.js The file is innclided in standard html <script...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
3
by: giloosh | last post by:
can i pass a hash as a function parameter. ive seen it been used before but i can't figure out how to do it. i would like to call a function like this for example ...
1
by: seth | last post by:
Hi: I'm trying to read JSON strings sent from the browser. Here is the scenario: 1. Using YUI tookit 2. sending JSON string from YUI toolkit - using the provided asyncRequest method. **I...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
1
by: bpejman | last post by:
Hi Everyone, I've been reading and searching the web for days trying to figure out how exactly you can access and read a Perl array or hash from within JavaScript. I've been reading that JSON is...
4
by: pbd22 | last post by:
hi, this has been a bugger for me. JSON: {" + escape('some (annoying) text') + " : "friendly text" } i want to escape the text inside the escape function but show the results inside double...
2
pradeepjain
by: pradeepjain | last post by:
i have a page post2.php which prints the json array like { page: 1, total: 239, rows: }, {id:'238',cell:}, {id:'237',cell:}, {id:'236',cell:}, {id:'235',cell:},
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.