2014-02-18 24 views
0

我已經從Yahoo郵件webservice中提取了json反應。之後,我解析使用播放json庫。從json字符串中提取郵件對象

現在我不無法遍歷並構建郵件對象的名單與發件人姓名,主題,ccList等

Mail對象我試圖構造是:列表(EmailMessage(主題,recvdDate, body1,sender,recipientsList))

我檢查了文檔play website。但是難以理解他們是scala學習者。有人可以幫助我,我們如何才能實現這種使用案例類。

迴應,我得到了格式的Web服務:

{ 
    "result": { 
     "folder": { 
      "folderInfo": { 
       "fid": "Sent", 
       "name": "Sent" 
      }, 
      "total": 3, 
      "unread": 0, 
      "size": 943522, 
      "isSystem": true 
     }, 
     "total": 3, 
     "code": [], 
     "message": [ 
      { 
       "mid": "2_0_0_2_3196_ACfai2IAACqmUwNFtgAAAKAUN2U", 
       "receivedDate": 1392723382, 
       "subject": "test mail", 
       "xapparentlyto": "", 
       "hasBlockedImages": false, 
       "flags": { 
        "isReplied": 0, 
        "isFlagged": 0, 
        "isRead": 1, 
        "isDraft": 0, 
        "isForwarded": 0, 
        "isHam": 0, 
        "isSpam": 0, 
        "hasAttachment": 0, 
        "isRecent": 1, 
        "inAddressBook": 0 
       }, 
       "from": { 
        "email": "[email protected]", 
        "name": "Rajeev Kumar Kallempudi" 
       }, 
       "to": [ 
        { 
         "email": "[email protected]", 
         "name": "[email protected]" 
        } 
       ], 
       "replyto": [ 
        { 
         "email": "[email protected]", 
         "name": "Rajeev Kumar Kallempudi" 
        } 
       ], 
       "cc": [], 
       "bcc": [], 
       "domainkey": false, 
       "messageId": "<[email protected]>", 
       "inReplyTo": "", 
       "references": "", 
       "sender": "[email protected]", 
       "part": [ 
        { 
         "partId": "HEADER", 
         "type": "x-unknown", 
         "subtype": "", 
         "typeParams": "", 
         "disposition": "", 
         "dispParams": "", 
         "encoding": "7bit", 
         "filename": "", 
         "size": 1474, 
         "contentId": "", 
         "isTruncated": false, 
         "hasBlockedImages": false, 
         "referencedInline": false 
        }      
       ] 
      }, 
      { 
       "mid": "2_0_0_2_2463_ACfai2IAAAdpUwM1NwAAAD0sJOA", 
       "receivedDate": 1392719159, 
       "subject": "passage1", 
       "xapparentlyto": "", 
       "hasBlockedImages": false, 
       "flags": { 
        "isReplied": 0, 
        "isFlagged": 0, 
        "isRead": 1, 
        "isDraft": 0, 
        "isForwarded": 0, 
        "isHam": 0, 
        "isSpam": 0, 
        "hasAttachment": 0, 
        "isRecent": 1, 
        "inAddressBook": 0 
       }, 
       "from": { 
        "email": "[email protected]", 
        "name": "Rajeev Kumar Kallempudi" 
       }, 
       "to": [ 
        { 
         "email": "[email protected]", 
         "name": "" 
        } 
       ], 
       "replyto": [ 
        { 
         "email": "[email protected]", 
         "name": "Rajeev Kumar Kallempudi" 
        } 
       ], 
       "cc": [], 
       "bcc": [], 
       "domainkey": false, 
       "messageId": "<[email protected]>", 
       "inReplyTo": "", 
       "references": "", 
       "sender": "[email protected]", 
       "part": [ 
        { 
         "partId": "HEADER", 
         "type": "x-unknown", 
         "subtype": "", 
         "typeParams": "", 
         "disposition": "", 
         "dispParams": "", 
         "encoding": "7bit", 
         "filename": "", 
         "size": 1949, 
         "contentId": "", 
         "isTruncated": false, 
         "hasBlockedImages": false, 
         "referencedInline": false 
        }, 
        { 
         "partId": "TEXT", 
         "subtype": "alternative", 
         "type": "multipart", 
         "typeParams": "boundary=\"-827237569-990831377-1392719159=:63976\"", 
         "disposition": "", 
         "dispParams": "", 
         "encoding": "7bit", 
         "filename": "", 
         "size": 11623, 
         "contentId": "", 
         "isTruncated": false, 
         "hasBlockedImages": false, 
         "referencedInline": false 
        }      
       ] 
      } 
     ] 
    }, 
    "error": null 
} 

回答

1

如果你只是查詢雅虎的消息,你不會得到電子郵件的內容,所以你應該使用一個查詢,看起來更像這一點。請注意,這將查詢帳戶的所有消息,包括已讀或已存檔的消息。

select 
    message.subject, 
    message.receivedDate, 
    message.from, 
    message.to, 
    message.cc, 
    message.bcc, 
    message.part.text 
from ymail.msgcontent 
where (fid,mids) in (
    select 
    folder.folderInfo.fid, 
    mid 
    from ymail.messages 
) 

一旦你有了這些,你只需要使用Play文檔中描述的JSON pick元素特性。它應該是這個樣子:

import java.util._ 

case class EmailMessage(
    subject  : String, 
    receivedDate : Date, 
    body   : String, 
    sender  : String, 
    recipients : List[String] 
) 

def yahooToEmailMessage(json:JsValue) = { 

    val subject = json/"result"/"message"/"subject" 
    val body = json/"result"/"message"/"part"/"text" 
    val ts = (json/"result"/"message"/"receivedDate").toLong 

    val receivedDate = new Date(ts * 1000L) 

    val sender = getRecipient(json/"result"/"message"/"to") 
    val to = getRecipients(json/"result"/"message"/"to") 
    val cc = getRecipients(json/"result"/"message"/"cc") 
    val bcc = getRecipients(json/"result"/"message"/"bcc") 

    val recipients = to ::: cc ::: bcc 

    EmailMessage(
    subject, 
    receivedDate, 
    body, 
    sender, 
    recipients 
) 
} 

def getRecipient(recipient:JsValue):List[String] = { 

    val name = recipient/"name" 
    val email = recipient/"email" 

    name + " <" + email + ">" 
} 

def getRecipients(array:JsArray):List[String] = { 
    array.foldLeft(List[String]()) { 
    (list,recipient) => list ::: List(getRecipient(recipient)) 
    } 
} 
+0

傑森, 做u有關於這個問題的任何想法:http://stackoverflow.com/questions/21935025/effective-way-to-process-yahoo-mails-using -yql – Rajeev