简体   繁体   English

在Ruby中如何将json中包含的json数组解析为类?

[英]In ruby how to parse json array contained in json into class?

I'm using the bubble wrap library which is working well to parse this json into classes of Item, the problem I have is how to extract the values of notifications. 我正在使用冒泡包装库,该库很好地将json解析为Item类,我遇到的问题是如何提取通知的值。 I've tried parsing the key/value for notifications the same way as the parent but I've been getting errors on the line that parses (Invalid Selector sent to 0x34..) I'm guessing it's no longer valid json once I read the member property only? 我曾尝试以与父项相同的方式解析通知的键/值,但在解析的行上一直出现错误(无效选择器已发送至0x34 ..)我猜测一旦我阅读它就不再是有效的json仅会员财产? Thanks! 谢谢!

{
"users": [
    {
        "id": 5,
        "username": "96u39nos9u",
        "password": "estanimiautem",
        "notifications": [
            {
                "notification": {
                    "body": "Eum dolorem aliquam animi ut."
                }
            },
            {
                "notification": {
                    "body": "verities"
                }
            }
        ]
    }
  ]
}

the snippet to parse into objects (works except for the notifications) 片段以解析为对象(除通知以外的其他作品)

BW::HTTP.get("http://resturl/api/v1/users") do |response|                                                              

  mydata = BW::JSON.parse(response.body.to_str)["users"]

  mydata.each {
    |item|
    aItem = Item.new(item)
    @data << aItem
  }

  @table.dataSource = self
  @table.delegate = self
  self.view.addSubview @table
end                                                                                                                                           

the class which converts json into properties 将json转换为属性的类

class Item                                                                                                                                                  
  PROPERTIES = [:id, :name, :status, :email, :notifications]
  PROPERTIES.each { |prop|
    p prop
    attr_accessor prop
  }

  def initialize(hash = {})
    p "initialize"
    p hash
    p "end initialize"
    hash.each { |key, value|
      p key
      p value
      if key.to_s == "notifications"
        // pass the values into a local array or separate class?
      end
      if PROPERTIES.member? key.to_sym
        self.send((key.to_s + "=").to_s, value)
      end
    }
  end                                                                                                                                                       
end    

I think a good start would be to remove all the unnecessary complication in your code. 我认为一个好的开始是消除代码中所有不必要的复杂性。 Then it will be a lot easier to diagnose what's going on. 然后,将很容易诊断发生了什么。

BW::HTTP.get("http://resturl/api/v1/users") do |response|                                                              

  mydata = BW::JSON.parse(response.body.to_s)["users"]

  @data = mydata.collect{|i| Item.new(i)}


  @table.dataSource = self
  @table.delegate = self
  self.view.addSubview @table
end  

And

class Item                                                                                                                                                  
  attr_accessor :id, :name, :status, :email, :notifications


  def initialize(hash = {})
    @id = hash['id']
    @name = hash['name']
    @status = hash['status']
    @email = hash['email']
    @notification = hash['notification']
  end                                                                                                                                                       
end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM