【问题标题】:Please help me access nested array请帮我访问嵌套数组
【发布时间】:2015-12-21 12:54:46
【问题描述】:

我有一个从 API 调用的数组。返回的数据是这样的(整理以供显示)。

[
    {
        "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 并返回此数据。

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 调用或可能的视图,但一直在尝试我在网上找到的不同变体,但似乎无法破解它。非常感谢任何帮助。

【问题讨论】:

  • 一般来说,共享足够少的代码来推动这一点是一个好主意——换句话说,展示你所面临的问题。对您的问题投反对票的原因可能是代码量过大,并且不清楚您面临的问题是什么。

标签: arrays ruby padrino


【解决方案1】:

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

而不是使用

@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

【讨论】:

  • 感谢您的意见。正如你所说的那样,但是向模型添加 options['Messages'][0]['IBMessageID'].to_s 给我带来了问题。再次取出它,它可以正常工作,所以除非您有任何理由保留它,否则请将其保留。无论如何它有效,所以感谢您的帮助。不敢相信它是如此简单,但重点是它以一种直截了当的方式进行了解释。
猜你喜欢
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-05-08
  • 2019-12-05
  • 2018-08-16
  • 1970-01-01
相关资源
最近更新 更多