简体   繁体   English

解析特定的字符串Javascript(Node.js)

[英]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 ... 为了访问可变datesubject等...

But I am not sure how to do it since it 但是我不确定该怎么做

  1. Is not a valid JSON 不是有效的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 ) 数据正在使用node-imap模块获取( 仅相关部分

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--. 参见@ stdob--的注释。 Imap.parseHeader() return an object. Imap.parseHeader()返回一个对象。

Looks like that Imap.parseHeader already returns an object with keys 看起来Imap.parseHeader已经返回带有键的对象

Try console.log( Object.keys(parsedHeader) to see all the keys. 尝试console.log( Object.keys(parsedHeader)查看所有键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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