Can't convert string to integer

Discussion in 'Ruby' started by Dennis Monsewicz, Aug 31, 2010.

  1. 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/.
     
    Dennis Monsewicz, Aug 31, 2010
    #1
    1. Advertising

  2. So how about looking at what json['member_id'] contains?
     
    Peter Hickman, Aug 31, 2010
    #2
    1. Advertising

  3. On Tue, Aug 31, 2010 at 5:08 PM, Dennis Monsewicz
    <> 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.
     
    Jesús Gabriel y Galán, Aug 31, 2010
    #3
  4. 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
    <> 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/.
    >
    >
     
    Steve Klabnik, Aug 31, 2010
    #4
  5. 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/.
     
    Dennis Monsewicz, Aug 31, 2010
    #5
  6. Dennis Monsewicz, Aug 31, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. les ander
    Replies:
    4
    Views:
    887
    wes weston
    Oct 5, 2004
  2. Replies:
    27
    Views:
    679
    Neil Cerutti
    Sep 29, 2006
  3. news
    Replies:
    2
    Views:
    1,112
  4. Une bévue

    can't convert String into Integer

    Une bévue, Feb 20, 2006, in forum: Ruby
    Replies:
    6
    Views:
    269
    Une bévue
    Feb 21, 2006
  5. Randy Kramer
    Replies:
    12
    Views:
    377
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page