473,767 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8173
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
Mar 14 '08 #2
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.
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_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
Mar 14 '08 #4
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.
Mar 14 '08 #5
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
Mar 14 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1589
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. Something like:
2
5501
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 src=json.js></script> fashion long story short. I have a function which will cycle through an associative array with a for loop and print out each key val pair of the hash table. The last entry I get is a key: toJSONstring with a value of function () { ......
2
3324
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 recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple...
3
3375
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 doSomething({variable1:"blabla",variable2:"blabla"}); how would function doSomething be written for this to work. I would also like the hash to be optional and the variables in the
1
2302
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 would like to read the JSON string sent from the client on the
23
3213
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 an object.
1
3621
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 the way to go, but non explain exactly how. If you can help me out, I would really appreciate it. Here's the scenario I have... I have a perl file called index.pl and in this file I have the below code. I know that arrays are converted to...
4
2331
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 quotes! The above line puts everything inside the double quotes and
2
4592
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
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10014
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.