Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Can't convert string to integer

Reply
Thread Tools

Can't convert string to integer

 
 
Dennis Monsewicz
Guest
Posts: n/a
 
      08-31-2010
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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Peter Hickman
Guest
Posts: n/a
 
      08-31-2010
So how about looking at what json['member_id'] contains?

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-31-2010
On Tue, Aug 31, 2010 at 5:08 PM, Dennis Monsewicz
<(E-Mail Removed)> 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.

 
Reply With Quote
 
Steve Klabnik
Guest
Posts: n/a
 
      08-31-2010
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 Monsewicz
<(E-Mail Removed)> 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!
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Dennis Monsewicz
Guest
Posts: n/a
 
      08-31-2010
I actually figured out my problem!

JSON.parse(resp.body).each do |r|
puts r["owner_id"]
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Dennis Monsewicz
Guest
Posts: n/a
 
      08-31-2010
Thanks for all of the help guys!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split string (then) Convert string into Integer news ASP General 2 05-26-2010 11:58 AM
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
Convert a Base32 string to Base10 integer Jon Paskett ASP .Net 1 12-18-2005 01:31 AM
convert scientific integer to normal integer les ander Python 4 10-05-2004 04:26 PM



Advertisments