如何从JSON中提取消息字符串

时间:2015-09-23 09:59:02

标签: ruby json

我正在尝试引用此JSON中包含的消息数组,但似乎无法获取它。

@cleanmessages = JSON.parse([@messages].to_json).first

我得到的最远的是:

 [  
   {  
      "IBMessageID"      =>1,
      "MessageThreadID"      =>1,
      "MessageText"      =>"Test Message",
      "FromCustomer"      =>true,
      "UserID"      =>nil,
      "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"      =>nil,
      "Date"      =>"2015-09-21T14:10:00      "}, 
   {
      "IBMessageID"=>16,
      "MessageThreadID"      =>1,
      "MessageText"      =>"Message 2",
      "FromCustomer"      =>true,
      "UserID"      =>nil,
      "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"      =>nil,
      "Date"      =>"2015-09-22T16:40:00      "}, 
    {
      "IBMessageID"=>27,
      "MessageThreadID"      =>1,
      "MessageText"      =>"test",
      "FromCustomer"      =>true,
      "UserID"      =>nil,
      "Date"      =>"2015-09-22T17:02:00      "}, 
    {
      "IBMessageID"=>28,
      "MessageThreadID"      =>1,
      "MessageText"      =>"test",
      "FromCustomer"      =>true,
      "UserID"      =>nil,
      "Date"      =>"2015-09-22T17:06:00
   }
]

返回:

127.0.0.1/test.png

如何引用每个数组中的每个元素。我想最终能够为每个线程单独显示每个元素。

2 个答案:

答案 0 :(得分:0)

这将为您提供包含按MessageThreadID

分组的消息的哈希值
@cleanmessages.group_by{ |msg_hash| msg_hash['MessageThreadID']}

答案 1 :(得分:0)

不清楚您实际想要的输出。

document = JSON.parse([@messages].to_json)
document.each do |thread|
    # gives you all text messages within the current thread: "Test Message", "Test this Update", ... 
    messages = thread['Messages'].map {|m| m['MessageText']} 

    # iterates over all messages in the current thread 
    thread['Messages'].each {|m| puts "Message #{m['IBMessageID']} from thread #{thread['MessageThreadID']}"}
end

一般来说, JSON.parse 不会返回特殊内容。它为相应的JSON数组返回数组,或为相应的JSON对象返回哈希