javascript - Parse js arrays in ruby -


i've js file holds array objects , data assigns

var a_1_val = new array(7); var b_1_txt = new array(7);           a_1_val[0] = '111'; b_1_txt[0] = 'utf8_content';  a_1_val[1] = '222'; b_1_txt[1] = 'bar'; 

etc..

need these arrays in ruby.

found http://github.com/jbarnette/johnson, can't correctly return array object

another way eval js in ruby, similar to

  1. get name of arrays

  2. cut arrays initialize js

  3. ruby eval

    a_1_val[0] = '111'

    b_1_txt[0] = 'utf8_content'

both ways sucks. maybe can suggests ideas

thanks

you can use json string marshal data between javascript , ruby:

#!/usr/bin/env ruby  require 'johnson' require 'open-uri' require 'yajl'  # grab source javascript json implementation json_js = open('http://www.json.org/json2.js').read # strip silly alert @ top of file json_js.gsub!(/^(alert.*)$/, '/* \1 */')  # javascript wanted some_js = <<-eof var a_1_val = new array(7); var b_1_txt = new array(7);           a_1_val[0] = '111'; b_1_txt[0] = 'Ähtäri';  a_1_val[1] = 'barsebäck slott'; b_1_txt[1] = '新宿区'; eof  result = johnson.evaluate(<<-eof) /* include json source code */ #{json_js}  /* include source code wanted */ #{some_js}  /* turn things wanted out string */ json.stringify([ a_1_val, b_1_txt ]) eof  # result in ruby ruby_result = yajl::parser.parse(result)  # puts ruby_result.inspect 

which gives output:

[["111", "barseb\303\244ck slott", nil, nil, nil, nil, nil], ["\303\204ht\303\244ri", "\346\226\260\345\256\277\345\214\272", nil, nil, nil, nil, nil]] 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -