最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

convert Javascript Object to a Ruby Hash - Stack Overflow

programmeradmin6浏览0评论

I have a javascript object of this form

 obj = "[
   {
     title: "Sean Kingston1",
     duration: parseInt("71", 10),
   },
   {
     title: "Sean Kingston2",
     duration: parseInt("71", 10),
   },
 ]"

is there a way to convert this to a ruby hash ?

I tried using JSON.parse and JSON.load
both of them throw

 JSON::ParserError: lexical error: invalid string in json text.
                               {   title: "Sean Kingston1
                 (right here) ------^

Is there a generic solution or should I use regex and then construct the hash in ruby ?

I have a javascript object of this form

 obj = "[
   {
     title: "Sean Kingston1",
     duration: parseInt("71", 10),
   },
   {
     title: "Sean Kingston2",
     duration: parseInt("71", 10),
   },
 ]"

is there a way to convert this to a ruby hash ?

I tried using JSON.parse and JSON.load
both of them throw

 JSON::ParserError: lexical error: invalid string in json text.
                               {   title: "Sean Kingston1
                 (right here) ------^

Is there a generic solution or should I use regex and then construct the hash in ruby ?

Share Improve this question edited Mar 4, 2014 at 8:40 vireshas asked Mar 6, 2012 at 8:16 vireshasvireshas 8166 silver badges19 bronze badges 7
  • 1 Deleting your old question and creating a new one doesn't change the fact that what you have is not valid JSON. – Andrew Marshall Commented Mar 6, 2012 at 8:43
  • 2 @andrew i did change the question its a javascript object and that is what i get from the server... do u know an answer to the question ?? – vireshas Commented Mar 6, 2012 at 9:10
  • @andrew i never used the word JSON! – vireshas Commented Mar 6, 2012 at 9:13
  • 1 You did when you tagged it as JSON. – Andrew Marshall Commented Mar 6, 2012 at 9:14
  • my bad,i didnt notice! what do u think i shld do here ? i will probably use reqex and convert all the title and duration to "title" and "duration".. is there a generic solution ? – vireshas Commented Mar 6, 2012 at 9:19
 |  Show 2 more ments

2 Answers 2

Reset to default 4

ruby 1.9 supports hash of this kind which resembles a javascript object

 obj = "[
  {
   title: "Sean Kingston1",
   duration: "71",
  },
  {
   title: "Sean Kingston2",
   duration: "71",
  },
 ]"

to convert this into a ruby hash

 eval(obj)

This is not a JSON. Actually, JSON is not the same as code, could be interpreted by javascript and evaluated to object.

JSON itself allows only static values (no parseInt) and any keys should be quoted as well.

[{
    "title": "Sean Kingston1",
    "duration": 71
 },
 {
    "title": "Sean Kingston2",
    "duration": 71
 }]

Using regexes and such things ain't good. You'd better just format JSON properly.

Ok, if you're not able to modify that input, you may solve a problem for this particular input with following regexpes:

/^\s*(\w+)\s*:/, '"\1":';
/:\s*parseInt\("(\d+)"\,\s*10)/, ': \1';

but for any variation in input you'll need to add more and more regexpes.

Generally, in order to interpret javascript you need to ... interpret javascript.

This is possible via installing some js Engine, like Rhino or V8 and binding it to Ruby.

发布评论

评论列表(0)

  1. 暂无评论