2013-02-01 41 views
-1

我使用續集和JSON寶石紅寶石JSON:在紅寶石ParseError

當我嘗試記錄轉換成JSON對象並試圖獲得其字段我的代碼是

require 'json' 
require "rubygems" 
require "sequel" 
require 'yaml' 
require "sequel/extensions/pg_array" 

DB = Sequel.connect('postgres://ritesh:[email protected]') 

pokes=DB[:use]; 
car = {:make => pokes.all, :year => "2003"} 
cp = pokes.filter(:id =>1).first.inspect 

jj = cp.to_json 
puts jj 
parseObject = JSON.parse(cp) 
在過去

行我得到錯誤

"{:id=>1, :first_name=>\"Ritesh\", :last_name=>\"Mehandiratta\", :email=>\"[email protected]\", :zipcode=>\"127021\", :comapnay_name=>\"heroku\", :google_profile=>\"google\", :skype=>\"helloworld\", :phone=>\"9013895056\", :about=>\"i am a great person\", :linkedin_profile_url=>\"linkedin.com\", :comapny_url=>\"company.com\", :needs=>[\"Web Designer\", \"Web Developer\", \"SoftWare Developer\"], :offering=>[\"Web Designer\", \"Web Developer\", \"SoftWare Developer\"], :upcoming_meetings=>[1, 2, 3, 4], :past_meetings=>[1, 2, 3, 4], :top_matches=>[1, 2, 3, 4]}" 
/home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/json/common.rb:148:in `parse': 743: unexpected token at '{:id=>1, :first_name=>"Ritesh", :last_name=>"Mehandiratta", :email=>"[email protected]", :zipcode=>"127021", :comapnay_name=>"heroku", :google_profile=>"google", :skype=>"helloworld", :phone=>"9013895056", :about=>"i am a great person", :linkedin_profile_url=>"linkedin.com", :comapny_url=>"company.com", :needs=>["Web Designer", "Web Developer", "SoftWare Developer"], :offering=>["Web Designer", "Web Developer", "SoftWare Developer"], :upcoming_meetings=>[1, 2, 3, 4], :past_meetings=>[1, 2, 3, 4], :top_matches=>[1, 2, 3, 4]}' (JSON::ParserError) 
    from /home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/json/common.rb:148:in `parse' 
    from hello1.rb:15:in `<main>' 

當我分析是這樣JJ變量的建議在回答

jj = cp.to_json 
puts jj 
parseObject = JSON.parse(jj) 

然後我收到錯誤

"{:id=>1, :first_name=>\"Ritesh\", :last_name=>\"Mehandiratta\", :email=>\"[email protected]\", :zipcode=>\"127021\", :comapnay_name=>\"heroku\", :google_profile=>\"google\", :skype=>\"helloworld\", :phone=>\"9013895056\", :about=>\"i am a great person\", :linkedin_profile_url=>\"linkedin.com\", :comapny_url=>\"company.com\", :needs=>[\"Web Designer\", \"Web Developer\", \"SoftWare Developer\"], :offering=>[\"Web Designer\", \"Web Developer\", \"SoftWare Developer\"], :upcoming_meetings=>[1, 2, 3, 4], :past_meetings=>[1, 2, 3, 4], :top_matches=>[1, 2, 3, 4]}" 
/home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/json/common.rb:148:in `parse': 743: unexpected token at '"{:id=>1, :first_name=>\"Ritesh\", :last_name=>\"Mehandiratta\", :email=>\"[email protected]\", :zipcode=>\"127021\", :comapnay_name=>\"heroku\", :google_profile=>\"google\", :skype=>\"helloworld\", :phone=>\"9013895056\", :about=>\"i am a great person\", :linkedin_profile_url=>\"linkedin.com\", :comapny_url=>\"company.com\", :needs=>[\"Web Designer\", \"Web Developer\", \"SoftWare Developer\"], :offering=>[\"Web Designer\", \"Web Developer\", \"SoftWare Developer\"], :upcoming_meetings=>[1, 2, 3, 4], :past_meetings=>[1, 2, 3, 4], :top_matches=>[1, 2, 3, 4]}"' (JSON::ParserError) 
    from /home/ritesh/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/json/common.rb:148:in `parse' 
    from hello1.rb:15:in `<main>' 

,而不是隻在字符串CP在JJ存在兩個誤區

'{:id=>1 
'"{:id=>1 

值得注意的差異,但誤差是相同

爲什麼我得到這個解析器異常錯誤這是一個有效的json string.i必須將其轉換爲哈希請告訴方式如何轉換爲哈希以及如何糾正這個錯誤

+0

你應該分析:JJ(IE)的parseObject = JSON.parse(JJ) – codeit

+0

JSON.parse(JJ)也將給予解析錯誤。因爲你先用'inspect'然後轉換成'json'。所以'檢查'也增加'斜線'。它造成了「解析」的不明確性。我認爲這可能是我的寶石中的錯誤。嘗試使用最新版本。 – codeit

+0

請看編輯當我按照你的指示發生什麼事 –

回答

0

我想你想解析JJ,而不是CP ...

jj = cp.to_json 
puts jj 
parseObject = JSON.parse(jj)