-1

我在 ruby​​ 中使用 sequel 和 json gem

当我尝试将记录转换为 json 对象并尝试获取其字段时,我的代码是

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

DB = Sequel.connect('postgres://ritesh:newpassword@localhost')

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=>\"riteshmehandiratta@gmail.com\", :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=>"riteshmehandiratta@gmail.com", :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=>\"riteshmehandiratta@gmail.com\", :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=>\"riteshmehandiratta@gmail.com\", :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 中的 cp 仅在字符串中存在差异

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

注意两个错误但错误相同

为什么我收到此解析器异常错误这是一个有效的 json 字符串。我必须将其转换为哈希请告诉如何转换为哈希以及如何纠正此错误的方法

4

1 回答 1

0

我想你想解析 jj 而不是 cp ......

jj = cp.to_json
puts jj
parseObject = JSON.parse(jj)
于 2013-02-01T19:22:56.133 回答