Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know that I can create an associative Array like this:

var MyAssocArray = {'sky':'blue', 'grass':'green'};

And I am very fond of using this method.

What I would like to do and am having trouble with is this:

I have strings saved like this:

var MyString = "'sky':'blue', 'grass':'green'";

And I want to be able to do this now:

var MyAssocArray = {MyString};

When I try that - I get this error:

invalid object initializer

What am I doing wrong? How can I achieve this? Any help would be greatly appreciated


Found different solution using PHP and Javascript. The Associative array string is echoed in the javascript

var Multidimensional_Arr[Multidimensional_Array_Key_Name] = {<?php echo $String_Pulled_From_Database; ?>}; // i.e. 'sky':'blue', 'grass':'green'

//Same can be done for single dimension array
var My_Single_Dime_Arr = {<?php echo $String_Pulled_From_Database; ?>}; // i.e. 'sky':'blue', 'grass':'green'

I hope it is helpful to someone

share|improve this question

3 Answers

Use JSON -- it's serialized Javascript Object Notation and is pretty close to what you're doing.

You'd need to do

var MyAssocArray = JSON.parse(MyString);

Also, JSON uses double quotes, not single quotes; if you use simple objects you can probably write code to just replace ' with "" in your strings, but it's tricky if the strings contain double-quotes.

edit: If you are using a browser that doesn't implement JSON.parse(), you can either use the implementation on the JSON website (see links at bottom of this page) or if you're using jQuery, there's jQuery.parseJSON()


Edit#2: Warning: Your solution has a security risk, unless you are sure the data in the database has been sanitized:

 var My_Single_Dime_Arr = {<?php echo $String_Pulled_From_Database; ?>}

This is equivalent to a call to eval(); if your database string has any malicious code it can do bad things. This is one reason why JSON was invented -- it's easy to ensure that its contents are valid (and hence safe) before evaluated.

Your overall architecture, as you have presented it to us, is [data in database] -> server-side PHP -> client-side Javascript. This is a classic example of serialized data. I realize you may have constraints to keep your system running without interruption, but a strict serialization format would make your system more secure.

share|improve this answer
+1 It seems to be the best available option. – Tadeck Mar 6 '12 at 3:04
Except, can the JSON object really be recommended without a browser support caveat yet? – jsumners Mar 6 '12 at 3:08
And now a +1 from me. – jsumners Mar 6 '12 at 14:00

You can't do that.

To achieve what you want use the split() function to split your string into comma separated tokens first. Then each token should further be split by the ':' character. Then push the two tokens obtained by the last split as key and value of your associative array.

share|improve this answer
that unfortunately won't work if any of the string keys or values has a comma in it. – Jason S Mar 6 '12 at 16:50
@JasonS good point – Sid Mar 6 '12 at 16:51

If you are sure the data is safe, then you could do:

var MyAssocArray = eval('{' + MyString + '}');
share|improve this answer
Also - I found another method:Since the string is being pulled out of a database - I found that I can populate the javascript associative array using PHP – user1005858 Mar 6 '12 at 5:35
Yeah.. this is not working for me. I tried it and get the error "-- invalid label" – user1005858 Mar 6 '12 at 7:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.