简体   繁体   中英

Parse specific String Javascript (Node.js)

I would like to parse the following String:

{ 
  date: [ 'Thu, 28 Apr 2016 10:56:13 +0200' ],
  subject: [ 'Subject' ],
  from: [ 'Blob <blob@test.com>' ],
  to: [ '<blab@test.com>' ] 
}

In order to access the variable date , subject etc ...

But I am not sure how to do it since it

  1. Is not a valid JSON
  2. It is not a structure I know

And I don't want to re-invent the wheel if a solution exist which I am not (yet) aware of.

Any ideas?

EDIT

Data are getting using a node-imap module ( only relevant part )

f.on('message', function(msg, seqno) {
   console.log('Message #%d', seqno);
   var prefix = '(#' + seqno + ') ';
   msg.on('body', function(stream, info) {
   var buffer = '';
   stream.on('data', function(chunk) {
   buffer += chunk.toString('utf8');
});
stream.once('end', function() {
   var parsedHeader = inspect(Imap.parseHeader(buffer));
   console.log('Author: '+parsedHeader);
});

SOLVED

See the comment of @stdob--. Imap.parseHeader() return an object.

Looks like that Imap.parseHeader already returns an object with keys

Try console.log( Object.keys(parsedHeader) to see all the keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM