如何在此哈希中获取嵌套哈希

时间:2015-09-24 11:15:20

标签: ruby parsing hash

我很难在此哈希中引用消息。

[
  {
    "MessageThreadID": 1,
    "CustomerID": 171,
    "MessageType": 1,
    "Subject": "Test Message",
    "OpenDate": "2015-09-17T00:00:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 1,
        "MessageThreadID": 1,
        "MessageText": "Test Message",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-17T17:23:00"
      },
      {
        "IBMessageID": 2,
        "MessageThreadID": 1,
        "MessageText": "Test this Update",
        "FromCustomer": false,
        "UserID": 1,
        "Date": "2015-09-17T17:23:00"
      },
      {
        "IBMessageID": 3,
        "MessageThreadID": 1,
        "MessageText": "My New Message",
        "FromCustomer": false,
        "UserID": 1,
        "Date": "2015-09-17T17:23:00"
      },
      {
        "IBMessageID": 4,
        "MessageThreadID": 1,
        "MessageText": "Reply",
        "FromCustomer": false,
        "UserID": 1,
        "Date": "2015-09-17T17:05:00"
      },
      {
        "IBMessageID": 5,
        "MessageThreadID": 1,
        "MessageText": "Some sensible shit",
        "FromCustomer": false,
        "UserID": 1,
        "Date": "2015-09-17T17:23:00"
      },
      {
        "IBMessageID": 14,
        "MessageThreadID": 1,
        "MessageText": "Message 2",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-21T14:10:00"
      },
      {
        "IBMessageID": 16,
        "MessageThreadID": 1,
        "MessageText": "Message 2",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-22T16:22:00"
      },
      {
        "IBMessageID": 25,
        "MessageThreadID": 1,
        "MessageText": "Added via abacus\r\n",
        "FromCustomer": false,
        "UserID": 1,
        "Date": "2015-09-22T16:22:00"
      },
      {
        "IBMessageID": 26,
        "MessageThreadID": 1,
        "MessageText": "sdsdsdsd",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-22T16:40:00"
      },
      {
        "IBMessageID": 27,
        "MessageThreadID": 1,
        "MessageText": "test",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-22T17:02:00"
      },
      {
        "IBMessageID": 28,
        "MessageThreadID": 1,
        "MessageText": "test",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-22T17:06:00"
      }
    ]
  },
  {
    "MessageThreadID": 5,
    "CustomerID": 171,
    "MessageType": 1,
    "Subject": "Unit Test Thread",
    "OpenDate": "2015-09-21T13:11:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 12,
        "MessageThreadID": 5,
        "MessageText": "Test Message",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-21T13:11:00"
      }
    ]
  },
  {
    "MessageThreadID": 11,
    "CustomerID": 171,
    "MessageType": 0,
    "Subject": "Test5",
    "OpenDate": "2015-09-22T14:39:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 22,
        "MessageThreadID": 11,
        "MessageText": "Test5",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-22T14:39:00"
      }
    ]
  },
  {
    "MessageThreadID": 13,
    "CustomerID": 171,
    "MessageType": 0,
    "Subject": "Test6",
    "OpenDate": "2015-09-22T14:41:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 24,
        "MessageThreadID": 13,
        "MessageText": "Test6",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-22T14:41:00"
      }
    ]
  },
  {
    "MessageThreadID": 12,
    "CustomerID": 171,
    "MessageType": 0,
    "Subject": "Test5",
    "OpenDate": "2015-09-22T14:41:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 23,
        "MessageThreadID": 12,
        "MessageText": "Test5",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-22T14:41:00"
      }
    ]
  },
  {
    "MessageThreadID": 14,
    "CustomerID": 171,
    "MessageType": 1,
    "Subject": "Test",
    "OpenDate": "2015-09-23T12:24:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 29,
        "MessageThreadID": 14,
        "MessageText": "Test",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-23T12:24:00"
      }
    ]
  },
  {
    "MessageThreadID": 15,
    "CustomerID": 171,
    "MessageType": 1,
    "Subject": "Accounts Test",
    "OpenDate": "2015-09-23T15:35:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 30,
        "MessageThreadID": 15,
        "MessageText": "Accounts Test",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-23T15:35:00"
      }
    ]
  },
  {
    "MessageThreadID": 16,
    "CustomerID": 171,
    "MessageType": 2,
    "Subject": "Savings ",
    "OpenDate": "2015-09-23T15:36:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 31,
        "MessageThreadID": 16,
        "MessageText": "Savings",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-23T15:36:00"
      }
    ]
  },
  {
    "MessageThreadID": 17,
    "CustomerID": 171,
    "MessageType": 2,
    "Subject": "Savings ",
    "OpenDate": "2015-09-23T15:37:00",
    "Closed": false,
    "ClosedDate": null,
    "Messages": [
      {
        "IBMessageID": 32,
        "MessageThreadID": 17,
        "MessageText": "Savings",
        "FromCustomer": true,
        "UserID": null,
        "Date": "2015-09-23T15:37:00"
      }
    ]
  }
]

我的初始通话由以下代码执行:

  def open_contact_messages(customer_id)

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

    @openmessages = {}
    @openmessages = response.map do |openmessage|
      Contact.new(openmessage) 
    end

    return @openmessages
  end  

上面显示的输出来自非常响应。在我的初始化程序中,我有以下代码。 ibmessages是我尝试引用消息中的数据,但我得到一个不能将字符串转换为整数错误。

class Contact
  attr_accessor :message_customer_ID, :message_type, :message_subject, :message_text, :message_source, :message_thread_ID
  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']

  end

end 

在我看来,我可以输出整个哈希,但引用消息哈希证明是困难的。

  - open_contact_messages(@customer.id).each do |openmessage|
    .message-container
      %p.message-intro
        Filler said....
      .message
        %h4.message-header.message-contents= "Message Subject : #{openmessage.message_subject}"
        %p.message-body.message-text= "Message Text : #{openmessage.message_text}"
        %p.message-body.message-source= "Message Source : #{openmessage.message_source}"
        %p.message-body.message-thread-ID= "Message Thread ID #{openmessage.message_thread_ID}"
        %p.message-body.message-thread-ID= "Message Test"
        %p.message-body.message-thread-ID
          - openmessage.messages do |message| 
            = message['MessageText']

我根据我在网上找到的许多方法得到了各种各样的错误但没有解决方案。最常见的错误是无法将整数转换为字符串。

1 个答案:

答案 0 :(得分:1)

这里有一个数组数组,即你有一堆消息线程,每个消息都包含一堆消息。

使用此代码

@openmessages = response.map do |openmessage|
  Contact.new(openmessage) 
end

您创建一个Contact实例,并为其提供一个消息线程,即此部分:

{
  "MessageThreadID": 1,
  "CustomerID": 171,
  "MessageType": 1,
  "Subject": "Test Message",
  "OpenDate": "2015-09-17T00:00:00",
  "Closed": false,
  "ClosedDate": null,
  "Messages": [ ... ]
}

在构造函数中,您可以直接从上面列出的options哈希值中分配,即

@message_customer_ID        = options['CustomerID'].to_s   # ok
@message_type               = options['MessageType'].to_s  # ok
@message_subject            = options['Subject'].to_s      # ok
@message_text               = options['MessageText'].to_s  # not ok
@message_source             = options['Closed'].to_s       # ok
@message_thread_ID          = options['MessageThreadID'].to_s # ok
@messages                   = options['Messages'].to_s     # not ok
@ibmessages                 = options['Messages']['IBMessageID'].to_s # not ok

Messages键包含一系列消息,因此您不能将它们转换为字符串,而是需要将它们存储为数组,以便您可以进一步处理它们。

如果要输出消息数组,则需要更改@messages分配给数组,如下所示:

@messages = options['Messages']

为您的Contact课程添加阅读器方法:

class Contact
  attr_reader :messages
end

之后,您可以在视图中使用此阅读器:

- openmessage.messages do |message|
  = message['MessageText']

请注意,在-行前面使用openmessage.messages do |message|,与我上面的代码完全相同,而不是=。它们之间的区别在于=将结果写入缓冲区(即输出内容),而-只运行一些ruby代码。在此上下文中,您不希望以序列化形式在视图中编写整个openmessage.messages数组,而是在循环中处理它。