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

Convert string to associative array

P: n/a
Hi All,

I have one small doubt regarding associative arrays. Request somebody
to clarify.

Is there a javascript utility function (like split) to convert a
string to associative array? My string is having elements in json
notation - in the form "{'MSRP': 20000, 'cashDown': 2000, 'tradeIn':
1000}"

Thank you,
Venkatesh

Feb 16 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Feb 16, 4:57 pm, "Venkatesh" <venkatbab...@gmail.comwrote:
Hi All,

I have one small doubt regarding associative arrays. Request somebody
to clarify.

Is there a javascript utility function (like split) to convert a
string to associative array? My string is having elements in json
notation - in the form "{'MSRP': 20000, 'cashDown': 2000, 'tradeIn':
1000}"
Javascript doesn't have associative arrays - it has objects with
properties and values, which gives them some features similar to
associative arrays.

What you seem to have is a JSON string, so you can use eval:

var obj = eval("{'MSRP':20000,'cashDown':2000,'tradeIn':1000 }");

--
Rob

Feb 16 '07 #2

P: n/a
>Is there a javascript utility function (like split) to convert a
>string to associative array? My string is having elements in json
notation - in the form "{'MSRP': 20000, 'cashDown': 2000, 'tradeIn':
1000}"

Javascript doesn't have associative arrays - it has objects with
properties and values, which gives them some features similar to
associative arrays.

What you seem to have is a JSON string, so you can use eval:

var obj = eval("{'MSRP':20000,'cashDown':2000,'tradeIn':1000 }");
There is a syntactic ambiguity in JavaScript that will cause that to fail.
Failure can be avoided by wrapping the string in parens, so that it will be
interpreted as an expression rather than a block.

var obj = eval('(' +
"{'MSRP': 20000,'cashDown': 2000,'tradeIn': 1000}" + ')');

http://www.JSON.org/
Feb 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.