【发布时间】:2013-02-01 19:18:55
【问题描述】:
我在 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 字符串。我必须将其转换为哈希,请告诉如何转换为哈希以及如何纠正此错误
【问题讨论】:
-
你应该解析:jj (i.e) parseObject = JSON.parse(jj)
-
JSON.parse(jj) 也会给出解析错误。因为您先使用
inspect,然后再转换为json。所以inspect也添加了slash。它给parse造成了歧义。我认为这可能是我的宝石中的错误。尝试使用最新版本。 -
请看编辑当我按照你的指示会发生什么
-
只需使用: cp = pokes.filter(:id =>1).first 。它会起作用的。
-
@checkit:它不会增加解析的歧义。它显然是无效的 JSON。有效的 JSON 对象不能是独立的字符串。它必须是一个数组或哈希/字典/对象。
标签: ruby-on-rails ruby json sequel