请帮我访问嵌套数组

Please help me access nested array

我有一个从 API 调用的数组。数据 returned 就像这样(为显示目的整理)。

[
    {
        "MessageThreadID": 30044,
        "CustomerID": 433,
        "MessageType": 1,
        "Subject": "What is my account balance?",
        "OpenDate": "2015-12-21T10:36:00",
        "Closed": false,
        "ClosedDate": null,
        "Messages": [
            {
                "IBMessageID": 30076,
                "MessageThreadID": 30044,
                "MessageText": "What is my account balance?",
                "FromCustomer": true,
                "UserID": null,
                "Date": "2015-12-21T10:36:00"
            },
            {
                "IBMessageID": 30077,
                "MessageThreadID": 30044,
                "MessageText": "£1230.00",
                "FromCustomer": false,
                "UserID": 1,
                "Date": "2015-12-21T10:36:00"
            },
            {
                "IBMessageID": 30078,
                "MessageThreadID": 30044,
                "MessageText": "Thanks you",
                "FromCustomer": true,
                "UserID": null,
                "Date": "2015-12-21T10:37:00"
            }
        ]
    }
]

据此,我在助手中使用以下内容来调用 API 和 return 此数据。

def contact_messages_threads(customer_id)

  customer_id = @customer.id
  response = get_call('/Messages/GetOpenedMessages/' + customer_id.to_s)
  response = JSON.parse(response.body)

  @openmessagethreads = {}
  @openmessagethreads = response.map do |openmessagethread|
    Contact.new(openmessagethread) 
  end

  return @openmessagethreads

end  

我的联系人模型如下,其中定义了不同的部分。

class Contact
  attr_accessor :message_customer_ID, :message_type, :message_subject, :message_source, :message_thread_ID, :messages, :messages_test, :message_text, :from_customer, 
  :message_user_id, :message_date, :openmessagethreads
  attr_reader :messages

  def initialize(options)

    @message_customer_ID        = options['CustomerID'].to_s 
    @message_type               = options['MessageType'].to_s 
    @message_subject            = options['Subject'].to_s 
    @message_source             = options['Closed'].to_s 
    @message_thread_ID          = options['MessageThreadID'].to_s 
    @messages                   = options['Messages']
    @messages_test              = options['Messages']['IBMessageID'].to_s
    @message_text               = options['MessageText']
    @from_customer              = options['FromCustomer']
    @message_user_id            = options['UserID']
    @message_date               = options['OpenDate']
    @openmessagethreads = {}

  end

end

最后在我看来,我这样调用数据:

- contact_messages_threads(@customer.id).each do |openmessagethread|
  = openmessagethread.message_subject

由此我可以很容易地调用 IBMessageThreadID、CustomerID 等数据,但是我无法访问 Messages 数组及其中包含的内容。在过去的两个月里,当我有空闲时间时,我一直在做这件事,但仍然无法破解它。我想我可能需要更改我的模型,api 调用或可能查看,但一直在尝试我在网上找到的不同变体,但似乎无法破解它。非常感谢任何帮助。

要访问数组,需要使用数组索引。

而不是使用

@messages_test              = options['Messages']['IBMessageID'].to_s

您需要使用,访问数组的第一个元素options['Messages'] 数组,下面的代码

@messages_test              = options['Messages'][0]['IBMessageID'].to_s

如果您愿意,可以使用

迭代数组
options['Messages'] each do |item|
    puts item["IBMessageID"]  # for illustration
end