如何在node.js中提取一个对象使用bert.js解码哪个对象?

时间:2011-12-16 10:56:33

标签: javascript node.js erlang bert-rpc

我有一个等同于此BERT的对象(为易读性而包装):

{"Hello",[{1,"john","john123"},{2,"Michale","michale123"}]} 
节点中的

var S = Bert.bytes_to_string([131,104,2,107,0,5,72,101,108,108,111,108,0,0,0,2,104,3,97,1,107,0,4,106,111,104,110,107,0,7,106,111,104,110,49,50,51,104,3,97,2,107,0,7,77,105,99,104,97,108,101,107,0,10,109,105,99,104,97,108,101,49,50,51,106]);
var Obj = Bert.decode(S);
console.log(obj);

我可以在控制台中看到如下。

{
  '0': {
    type: 'bytelist',
    value: 'Hello',
    toString: [ Function ],
    repr: [ Function ]
  },
  '1': [
    {
      '0': 1,
      '1': [ Object ],
      '2': [ Object ],
      type: 'tuple',
      length: 3,
      value: [ Object ],
      repr: [ Function ],
      toString: [ Function ]
    },
    {
      '0': 2,
      '1': [ Object ],
      '2': [ Object ],
      type: 'tuple',
      length: 3,
      value: [ Object ],
      repr: [ Function ],
      toString: [ Function ]
    }
  ],
  type: 'tuple',
  length: 2,
  value: [
    {
      type: 'bytelist',
      value: 'Hello',
      toString: [ Function ],
      repr: [ Function ]
    },
    [
      [ Object ],
      [ Object ]
    ]
  ],
  repr: [ Function ],
  toString: [ Function ]
}

如何将输出作为

{"Hello",[{1,"john","john123"},{2,"Michale","michale123"}]} 

来自obj?

1 个答案:

答案 0 :(得分:3)

使用

Obj['0'].value

获取字符串“hello” 和

Obj['1'].forEach(function(el){
  var fst = el['0'] //1,2
  var snd = el['1'].value //John,Mihcale
  var thd = el['2'].value  //john123,michale123
})

您只读“[Object]”,因为console.log不会深入到对象层次结构中,如果您想要显示所有属性,请尝试以下操作:

console.log(require('util').inspect(Obj,false,100))