I have a data-type element for each option in select box. When I select an option I get the data-type as "[{:type=>"textbox", :label=>"Age"}]" (string of array of hashes). I want to convert this to array of hashes. I searched in google but couldn't get any. Can anyone help.
I tried replace as well,
str.replace('"[', '[').replace(']"' , ']'); but didnt get result. I want this in Javascript.
asked Sep 19, 2014 at 5:09
Kranthi
1,4172 gold badges18 silver badges35 bronze badges
-
but that is not coming in JSON format. SO am not able to acheive that.Kranthi– Kranthi2014年09月19日 05:12:42 +00:00Commented Sep 19, 2014 at 5:12
-
Is that what the value actually looks like? What kind of syntax is that? It's not JSON and it doesn't look like PHP either. Sounds to me like you need a bonafide parser, and that may be outside the scope of an SO question.JLRishe– JLRishe2014年09月19日 05:12:51 +00:00Commented Sep 19, 2014 at 5:12
1 Answer 1
Works for this one sample anyway:
var a = '[{:type=>"textbox", :label=>"Age"}]';
var b = JSON.parse(a.replace(/:/g, '"').replace(/=>/g,'":'));
console.table(b);
answered Sep 19, 2014 at 5:17
Malk
12k4 gold badges35 silver badges34 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Kranthi
what if I get string as "[{:type=>"textbox", :label=>"Age"}]"; ? because javascript is giving me string in the above way
default