【问题标题】:Parse JSON from HTTParty using Ruby on Rails使用 Ruby on Rails 从 HTTParty 解析 JSON
【发布时间】:2019-06-11 06:37:27
【问题描述】:

我正在使用HTTparty gem 将数据从JSON 文件实现到Ruby on Rails 应用程序,但我收到错误no implicit conversion of String into Integer

我参考了这个教程link

我的json数据,

{
    "restaurant_name": "Restaurant 3",
    "address": "xyz address",
    "country": "United States",
    "currency": "USD",
    "client_key": "12345",
    "client_name": "Client 3",
    "client_email": "test3@mail.com",
    "client_phone": "9876",
    "product_tier": "tier1",
    "brand_logo_large": {
        "ID": 37,
        "id": 37,
        "title": "bait-al-bahar-logo-design",
        "filename": "bait-al-bahar-logo-design.png",
        "filesize": 105071,
        "url": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
        "link": "http://codekyt.in/froodle-wp/projects/res-1-client-1/bait-al-bahar-logo-design/",
        "alt": "",
        "author": "1",
        "description": "",
        "caption": "",
        "name": "bait-al-bahar-logo-design",
        "status": "inherit",
        "uploaded_to": 35,
        "date": "2019-01-04 11:11:48",
        "modified": "2019-01-04 11:13:01",
        "menu_order": 0,
        "mime_type": "image/png",
        "type": "image",
        "subtype": "png",
        "icon": "http://codekyt.in/froodle-wp/wp-includes/images/media/default.png",
        "width": 600,
        "height": 500,
        "sizes": {
            "thumbnail": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-150x150.png",
            "thumbnail-width": 150,
            "thumbnail-height": 150,
            "medium": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-300x250.png",
            "medium-width": 300,
            "medium-height": 250,
            "medium_large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
            "medium_large-width": 600,
            "medium_large-height": 500,
            "large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
            "large-width": 600,
            "large-height": 500
        }
    }

在模型中,

include HTTParty

在控制器中

def index  
  require 'httparty'
  @category = HTTParty.get(
    'http://codekyt.in/froodle-wp/wp-json/data/v2/projects?client_key=12345',
    :headers =>{'Content-Type' => 'application/json'}
  )
end

在gemfile中,

gem 'httparty'
gem 'json'

在视图中,

<%@category.each do |category|%>
<%=category["restaurant_name"]%>
<%=category["country"]%>
<%=category["currency"]%>
<%=category["usd"]%>
<%=category["client_key"]%>
<%=category["client_name"]%>
<%=category["client_email"]%>
<%=category["client_phone"]%>
<%=category["product_tier"]%>
<%=category["brand_logo_large"]%>
<%end%>

【问题讨论】:

    标签: ruby-on-rails json ruby api


    【解决方案1】:

    HTTParty.get返回一个封装的响应类型为HTTParty::Response,你需要从中获取parsed response

    response = HTTParty.get(
      'http://codekyt.in/froodle-wp/wp-json/data/v2/projects?client_key=12345',
      :headers =>{'Content-Type' => 'application/json'}
    )
    @category = response.parsed_response # this will return the json.
    

    另外,在您的情况下,您不需要迭代@category,json 对象是单数的,您可以直接使用它:

    <%=@category["restaurant_name"]%>
    <%=@category["country"]%>
    <%=@category["currency"]%>
    <%=@category["usd"]%>
    <%=@category["client_key"]%>
    <%=@category["client_name"]%>
    <%=@category["client_email"]%>
    <%=@category["client_phone"]%>
    <%=@category["product_tier"]%>
    <%=@category["brand_logo_large"]%>
    

    【讨论】:

    • @kiddorails no my json object is not sigular 例如目的我只显示一个数据
    • 在这种情况下你可以迭代。但是您提供的和提到的 URL 上的示例 json 是单数的。
    • @kiddorails 现在遇到同样的错误,没有将字符串隐式转换为整数。
    • @rajD 在哪一行?在 parsed_response 中? response.code 给你什么?
    • 我用这种方式但同样的错误:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多