Can't convert string to integer

I am a newbie to Ruby so I apologize for my ignorance.

I am trying to parse a JSON object but I keep getting the error of Can’t
convert string to integer.

json = JSON.parse(resp.body)
json[‘member_id’].to_i

I have NO idea why this won’t work! Thanks in advance for the help!

So how about looking at what json[‘member_id’] contains?

On Tue, Aug 31, 2010 at 5:08 PM, Dennis M.
[email protected] wrote:

I am a newbie to Ruby so I apologize for my ignorance.

I am trying to parse a JSON object but I keep getting the error of Can’t
convert string to integer.

json = JSON.parse(resp.body)
json[‘member_id’].to_i

I have NO idea why this won’t work! Thanks in advance for the help!

Can you show us what resp.body is?
The following works for me:

irb(main):001:0> require ‘json’
=> true
irb(main):003:0> h = {‘member_id’ => 5, ‘name’ => ‘test’}
=> {“name”=>“test”, “member_id”=>5}
irb(main):005:0> h.to_json
=> “{“name”:“test”,“member_id”:5}”
irb(main):006:0> json = JSON.parse(h.to_json)
=> {“name”=>“test”, “member_id”=>5}
irb(main):007:0> json[“member_id”].to_i
=> 5

So, I think we need more info.

Jesus.

I actually figured out my problem!

JSON.parse(resp.body).each do |r|
puts r[“owner_id”]
end

Thanks for all of the help guys!

I’d suggest running this code:

json = JSON.parse(resp.body)
puts “member id is: ‘#{json[‘member_id’}’”

And seeing what that gives you.

On Tue, Aug 31, 2010 at 11:08 AM, Dennis M.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs