转换JSON号to_i返回1

时间:2014-01-10 06:16:01

标签: ruby-on-rails ruby json

我得到了这个哈希:

{
    "item": {
        "icon": "http://services.runescape.com/m=itemdb_rs/4332_obj_sprite.gif?id=4798",
        "icon_large": "http://services.runescape.com/m=itemdb_rs/4332_obj_big.gif?id=4798",
        "id": 4798,
        "type": "Ammo",
        "typeIcon": "http://www.runescape.com/img/categories/Ammo",
        "name": "Adamant brutal",
        "description": "Blunt adamantite arrow...ouch",
        "current": {
            "trend": "neutral",
            "price": 227
        },
        "today": {
            "trend": "neutral",
            "price": 0
        },
        "day30": {
            "trend": "positive",
            "change": "+1.0%"
        },
        "day90": {
            "trend": "positive",
            "change": "+1.0%"
        },
        "day180": {
            "trend": "positive",
            "change": "+2.0%"
        },
        "members": "true"
    }
}

我得到这样的当前价格:

class GpperxpController < ApplicationController
  def index
  end

  def cooking
    require 'open-uri'

    @sharkid = '385'
    @sharkurl = "http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=#{@sharkid}"
    @sharkpage = Nokogiri::HTML(open(@sharkurl))
    @sharkinfo = JSON.parse(@sharkpage.text)
    @sharkinfo = @sharkinfo['item']['current']['price']
  end
end
我视图中的

<%= @sharkinfo %>返回227.但是,我想对它执行一些数学运算,这就是我必须使用.to_i的原因。唯一的问题是,当我追加.to_i时,值会更改为1.为什么会这样?

2 个答案:

答案 0 :(得分:1)

给定json(http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=385)中的价格包含,

... "current":{"trend":"neutral","price":"1,844"},...
                                           ^

在致电,之前删除String#to_i

"1,844".to_i
# => 1
"1,844".gsub(',', '').to_i
# => 1844

答案 1 :(得分:1)

只运行irb,并将你的JSON响应放在一个变量中,我将响应变为227就没问题了,要么将价格拉出文本然后转换为整数,要么将价格拉出整数一举一动。

所以我的初始代码如下:

json_text = '''
{
"item": {
    "icon": "http://services.runescape.com/m=itemdb_rs/4332_obj_sprite.gif?id=4798",
    "icon_large": "http://services.runescape.com/m=itemdb_rs/4332_obj_big.gif?id=4798",
    "id": 4798,
    "type": "Ammo",
    "typeIcon": "http://www.runescape.com/img/categories/Ammo",
    "name": "Adamant brutal",
    "description": "Blunt adamantite arrow...ouch",
    "current": {
        "trend": "neutral",
        "price": 227
    },
    "today": {
        "trend": "neutral",
        "price": 0
    },
    "day30": {
        "trend": "positive",
        "change": "+1.0%"
    },
    "day90": {
        "trend": "positive",
        "change": "+1.0%"
    },
    "day180": {
        "trend": "positive",
        "change": "+2.0%"
    },
    "members": "true"
}
'''
require 'json'
si = JSON.parse(json_text)

然后是以下任何一种:

p = si['item']['current']['price']
price = p.to_i

price = si['item']['current']['price'].to_i

将价值227放在我的价格变量中。

如果我是你,我会避免使用相同的变量名称来表达不同的东西。如果您想要的是@sharkinfo中的整数价格,那么您最好使用临时名称(不带@符号)将价格作为文本输入,然后将整数值分配给所需的变量。

试试这个,看看是否有帮助。我会尝试监视这一点,看看你是否到达任何地方。此外,在您从JSON中提取文本时,我相信这不再是JSON问题。最后,您可能会包含什么版本的ruby以及您正在使用的平台(Windows / Mac / Linux /等)。