nodejson解析.issue

nodejson解析.issue,json,node.js,parsing,Json,Node.js,Parsing,我试图解析一个JSON来获得一个特定的行。我在谷歌上尝试了很多我能找到的不同的东西,这是我能找到的最接近的 我想读的是: [{ "id": "pinkcoin", "name": "PinkCoin", "symbol": "PINK", "rank": "321", "price_usd": "0.0281999", "price_btc": "0.00000165", "24h_volume_usd": "195433.0"

我试图解析一个JSON来获得一个特定的行。我在谷歌上尝试了很多我能找到的不同的东西,这是我能找到的最接近的

我想读的是:

[{
    "id": "pinkcoin", 
    "name": "PinkCoin", 
    "symbol": "PINK", 
    "rank": "321", 
    "price_usd": "0.0281999", 
    "price_btc": "0.00000165", 
    "24h_volume_usd": "195433.0", 
    "market_cap_usd": "10470475.0", 
    "available_supply": "371294750.0", 
    "total_supply": "388294750.0", 
    "max_supply": null, 
    "percent_change_1h": "5.48", 
    "percent_change_24h": "10.83", 
    "percent_change_7d": "-7.62", 
    "last_updated": "1513043947"
}]
我正试图从中提取“价格\美元”部分。。。下面是使用的代码im:

var request = require('request');
request('https://api.coinmarketcap.com/v1/ticker/pinkcoin/', function (error, response, body) {
  fs.readFile(body, 'utf8', function (err, data) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }

    data = JSON.parse(data);

    bot.sendMessage({
      to: channelID,
      message: data.price_usd
    });
  });
});
但当我触发此代码时,我在控制台中得到:

Error: Error: ENAMETOOLONG: name too long, open '[{
    "id": "pinkcoin",
    "name": "PinkCoin",
    "symbol": "PINK",
    "rank": "319",
    "price_usd": "0.0284066",
    "price_btc": "0.00000166",
    "24h_volume_usd": "195093.0",
    "market_cap_usd": "10547221.0",
    "available_supply": "371294750.0",
    "total_supply": "388294750.0",
    "max_supply": null,
    "percent_change_1h": "6.15",
    "percent_change_24h": "11.55",
    "percent_change_7d": "-6.97",
    "last_updated": "1513044245"
}]'

我一直想解决这个问题,但我一事无成……

我不知道你为什么要在那里使用
fs.readFile
。您将获取返回的整个JSON对象的结果,并将其用作的
path
参数。这个JSON对象是一个非常长的字符串,它比允许的文件路径长,因此抛出
ENAMETOOLONG

request
中的
body
值应该已经具有所需的JSON。除非您希望根据coinmarketcap API响应中的值从文件系统中读取某些文件,否则请使用
fs
删除该部分

编辑:另外,作为奖励,您也没有正确使用结果。它将返回一个数组,其中第一个对象具有您的结果。我不完全确定它在什么情况下返回多个值。所以你想要这个:

bot.sendMessage({
  to: channelID,
  message: data.price_usd
});
。。。看起来像这样:

bot.sendMessage({
  to: channelID,
  message: data[0].price_usd
});