2017-02-01 60 views
0

在Node.js中的bot上工作,它擴展了由另一個bot創建的數據。 Bot將其所有數據輸出到JSON頁面https://mee6.xyz/levels/267482689529970698?json=1獲取JSON流的內容

但是我看不到由JSONStream生成的數據的控制檯輸出。 我怎樣才能得到它,所以我可以將它用於我的擴展系統?

var request = require('request') 
    , JSONStream = require('JSONStream') 
    , es = require('event-stream') 

request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'}) 
    .pipe(JSONStream.parse('rows.*')) 
    .pipe(es.mapSync(function (data) { 
    console.error(data) 
var stream = JSONStream.parse(['rows', true, 'doc']) //rows, ANYTHING, doc 

stream.on('data', function(data) { 
    console.log('received:', data); 
}); 
//emits anything from _before_ the first match 
stream.on('header', function (data) { 
    console.log('header:', data) // => {"total_rows":129,"offset":0} 
}) 
    })) 

回答

1

有幾個問題。 看來你已經混合了JSONStream文檔中描述的2種方法。

首先,你的要求根本不包含名爲「行」這就是爲什麼這不起作用任何領域的JSON:.pipe(JSONStream.parse('rows.*'))

要見你可以做這樣的事情的輸出:

request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'}) 
 
\t //So I'm getting all the players records 
 
\t .pipe(JSONStream.parse('players.*')) 
 
\t .pipe(es.mapSync(function (data) { 
 
\t \t console.log(data); 
 
\t }));

結帳JSONStream和JSONPath文檔。

第二個是,這個流stream = JSONStream.parse(['rows', true, 'doc'])被創建,然後簡單地丟失。你注意到使用它。 所以,如果你不喜歡1號的方式,你可以這樣做:

var stream = JSONStream.parse(['players']); 
 
stream.on('data', function(data) { 
 
\t console.log('received:', data); 
 
}); 
 

 
stream.on('header', function (data) { 
 
\t console.log('header:', data); 
 
}); 
 

 
//Pipe your request into created json stream 
 
request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'}) 
 
\t .pipe(stream);

希望這有助於。

+0

第一個是完美的,是的我想我在某個地方犯了一個愚蠢的錯誤。我把最後一行改爲console.log(data.name);並且只是輸出玩家的名字,只需要找到一種方法來對它們進行排序,這樣我就可以將data.id與我的database.id中的匹配進行比較。 要繼續修補,以查看是否可以將它們排序成一個數組或不適合。謝謝! –