簡體   English   中英

如何解析節點js中的JSON?

[英]How to parse JSON in node js?

我正在使用API​​的節點包裝器: https//github.com/MySportsFeeds/mysportsfeeds-node/blob/master/README.md https://www.mysportsfeeds.com/data-feeds/api-docs#

通話正常,並在“/結果”下自動保存

這是我的代碼:

 msf.authenticate("username", "password");
    var data = msf.getData('nba', '2016-2017-regular', 'cumulative_player_stats', 'json', {
        player: 'nick-young'
    });

    request(data, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var parsedData = JSON.parse(body);
            console.log(parsedData["cumulativeplayerstats"]["playerstatsentry"][0]["stats"]["PtsPerGame"]["#text"]);
        }
    });

提前致謝

當您使用格式'json'調用msf.getData (聯賽,季節,Feed,格式以及Feed的任何其他適用參數)時。 它返回一個json對象。 因此,您的數據將是一個json對象。

msf.authenticate("username", "password");
var data = msf.getData('nba', '2016-2017-regular', 'cumulative_player_stats', 'json', {player: 'nick-young'});

console.log(data["cumulativeplayerstats"]["playerstatsentry"][0]["stats"]["PtsPerGame"]["#text"]);

使用fs.readFile讀取json文件內容

同步

const fs = require('fs');
const json = JSON.parse(fs.readFileSync('results/cumulative_player_stats-nba-2016-2017-regular.json', 'utf8'));

異步

const fs = require('fs');

fs.readFile('results/cumulative_player_stats-nba-2016-2017-regular.json', 'utf8', (err, data) => {
  if (err) throw err;

  const json = JSON.parse(data);

  console.log(json["cumulativeplayerstats"]["playerstatsentry"][0]["stats"]["PtsPerGame"]["#text"]);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM