0

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
2
  • but that is not coming in JSON format. SO am not able to acheive that. Commented 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. Commented Sep 19, 2014 at 5:12

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

what if I get string as "[{:type=>"textbox", :label=>"Age"}]"; ? because javascript is giving me string in the above way

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.