本文介绍了使用Ruby on Rails从HTTParty解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementation of data from a JSON file to a Ruby on Rails application by using HTTparty gem, but I am getting Error no implicit conversion of String into Integer

我参考了本教程链接

我的json数据

{
    "restaurant_name": "Restaurant 3",
    "address": "xyz address",
    "country": "United States",
    "currency": "USD",
    "client_key": "12345",
    "client_name": "Client 3",
    "client_email": "[email protected]",
    "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%>

推荐答案

HTTParty.get返回封装的类HTTParty::Response的响应类型,您需要通过以下方式从中检索parsed response:

HTTParty.get returns an encapsulated response type of class HTTParty::Response, you need to retrieve the parsed response from that by:

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对象是单个的,您可以直接使用它:

Also you do not need to iterate on @category in your case, the json object is singular and you can directly use it:

<%=@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"]%>

这篇关于使用Ruby on Rails从HTTParty解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:26