2015-09-06 31 views
0

你如何处理来自谷歌的JSON响应?googles recaptcha(version 2)回复

这是我得到的JSON:

res.on('data', function (chunk) { 
      //process.stdout.write(chunk);//formats it like I need it 

      var lines =JSON.parse(chunk); 
}); 

我从谷歌(例如),谷歌的JSON看起来像得到什么:

{ success: false, 'error-codes': [ 'missing-input-response' ] } 

我想会的工作:

JSON.parse("{ success: false, 'error-codes': [ 'missing-input-response' ] }").success; 

当然,它不起作用,因为它不适合格式化。

什么实际工作(但我需要改变,从谷歌的JSON):

JSON.parse("{ "success": false, "error-codes": [ "missing-input-response" ]}").success 

后来我发现this

var req = http.request(options, function(res) { 
    res.setEncoding('utf8'); 
    res.on('data', function(chunk) {//chunk is the JSON from google 
    var lines = chunk.split("\n"); 
    if(lines.length >= 2) { 
     if(lines[0] == 'true') 
      that._recaptcha_response.is_valid = true; 
     that._recaptcha_response.error = lines[1]; 
    } 
    that.emit('data', that._recaptcha_response); 
    }); 
}); 

但这似乎并没有对我的工作的地方,也许他们把JSON从recaptcha版本1更改为版本2?!。

修订

从深层次看结果如下:

Look here

+0

已更新我的帖子 – user254197

回答

0

它看起来像JSON可能已经被解析为您服务。 { success: false, 'error-codes': [ 'missing-input-response' ] }是Node.js如何打印出对象。

0

好吧,这解决了我的问题:

JSON.parse(blal.replace(/\n|\r/g, "")).success; 

假设blal是网上搜寻JSON。