ruby on rails - using eval to convert string into hash -
i have string this
a="{\"company\"=>\"khkhkh\", \"email\"=>\"hjkh@hkkj.kjh\",\"address\"=>\"yiyiyu\", \"date\"=>mon, 28 apr 2014 13:14:10 bst +01:00}"
but have made mistake when generating string. is, appended date without converting string. if try hash using eval(a) . throws error, acceptable.
syntaxerror: (eval):1: syntax error, unexpected tconstant, expecting => ....
is there way hash back, since iam in situation cant regenerate string.
any highly appreciable. thanks.
for immediate predicament:
a.gsub(/\"date\"\s*=>(.*?)(\}|,\s*\")/, '"date"=>"\1"\2')
this should work if date not last entry of hash.
for next time around: really not idea serialize data manually turning them code eval
later. in cases best bet generate json , parse later. using proper json serializer/generator make sure data syntactically correct.
Comments
Post a Comment