Can't convert string to integer

D

Dennis Monsewicz

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!
 
J

Jesús Gabriel y Galán

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.
 
S

Steve Klabnik

I'd suggest running this code:

json = JSON.parse(resp.body)
puts "member id is: '#{json['member_id'}'"

And seeing what that gives you.
 
D

Dennis Monsewicz

I actually figured out my problem!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top