HTTParty - JSON 到强类型对象

发布于 2025-01-04 07:31:55 字数 462 浏览 1 评论 0原文

是否可以让 HTTParty 将 GET 的结果反序列化为强类型 ruby​​ 对象?例如

class Myclass
 include HTTParty

end

x = Myclass.get('http://api.stackoverflow.com/1.0/questions?tags=HTTParty')

puts x.total
puts x.questions[0].title

,现在它将其反序列化为哈希值。

puts x["total"]

我的问题实际上是 HTTParty 是否支持此 OTB,而不是通过安装额外的 gem。

编辑

我对 Ruby 还很陌生,但我记得类字段都是私有的,因此需要通过 getter/setter 方法来访问它们。那么也许这个问题不是一个有效的问题?

Is it possible to have HTTParty deserialize the results from a GET to a strongly typed ruby object? For example

class Myclass
 include HTTParty

end

x = Myclass.get('http://api.stackoverflow.com/1.0/questions?tags=HTTParty')

puts x.total
puts x.questions[0].title

Right now it deserializes it into a hash

puts x["total"]

My question is actually if HTTParty supports this OTB, not by installing additional gems.

Edit:

I'm still new to Ruby, but I recall that class fields are all private so they would need to be accessed through getter/setter methods. So maybe this question isn't a valid one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2025-01-11 07:31:56

如果您只需要方法语法,则可以使用开放结构。

require 'httparty'
require 'ostruct'

result = HTTParty.get 'http://api.stackoverflow.com/1.0/questions?tags=HTTParty'
object = OpenStruct.new result
object.total # => 2634237

一个可能的缺点是这个对象是完全开放的,如果你调用它不存在的方法,它只会返回 nil (如果你调用 setter,它将创建 setter 和 getter)

If you are just wanting method syntax, you can use an open struct.

require 'httparty'
require 'ostruct'

result = HTTParty.get 'http://api.stackoverflow.com/1.0/questions?tags=HTTParty'
object = OpenStruct.new result
object.total # => 2634237

A possible downside is that this object is totally open such that if you invoke a nonexistent method on it, it will just return nil (if you invoke a setter, it will create both the setter and getter)

手心的温暖 2025-01-11 07:31:56

听起来您希望 Myclass::get 的返回值是 Myclass 的实例。如果是这种情况,您可以缓存 HTTP 请求的返回值并实现 method_missing 以从该哈希返回值:

class Myclass
  include HTTParty

  attr_accessor :retrieved_values

  def method_missing(method, *args, &block)
    if retrieved_values.key?(method)
      retrieved_values[method]
    else
      super
    end
  end

  def self.get_with_massaging(url)
    new.tap do |instance|
      instance.retrieved_values = get_without_massaging(url)
    end
  end

  class << self
    alias_method :get_without_massaging, :get
    alias_method :get, :get_with_massaging
  end
end

这并不完全是您所要求的,因为它只适用于一层深度 -即, x.questions[0].title 需要是 x.questions[0][:title]

x = Myclass.get('http://api.stackoverflow.com/1.0/questions?tags=HTTParty')
p x.total
p x.questions[0][:title]

也许你可以想出这个答案和约书亚溪的优势开放结构。

我还应该指出,如果您的方法没有被命名为 get,则所有方法别名欺骗都是不必要的。

It sounds like you want the return value of Myclass::get to be an instance of Myclass. If that's the case, you could cache the return value from the HTTP request and implement method_missing to return values from that hash:

class Myclass
  include HTTParty

  attr_accessor :retrieved_values

  def method_missing(method, *args, &block)
    if retrieved_values.key?(method)
      retrieved_values[method]
    else
      super
    end
  end

  def self.get_with_massaging(url)
    new.tap do |instance|
      instance.retrieved_values = get_without_massaging(url)
    end
  end

  class << self
    alias_method :get_without_massaging, :get
    alias_method :get, :get_with_massaging
  end
end

This isn't exactly what you asked for, because it only works one level deep — i.e., x.questions[0].title would need to be x.questions[0][:title]

x = Myclass.get('http://api.stackoverflow.com/1.0/questions?tags=HTTParty')
p x.total
p x.questions[0][:title]

Perhaps you could come up with some hybrid of this answer and Joshua Creek's to take advantage of OpenStruct.

I should also point out that all the method aliasing trickery isn't necessary if your method doesn't have to be named get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文