Javascript 节点获取JSON问题
我目前正在为我的discord机器人开发计算器功能。我发出了一个命令,获取steam市场项目价格,然后根据以下公式计算:Javascript 节点获取JSON问题,javascript,node.js,discord.js,node-fetch,Javascript,Node.js,Discord.js,Node Fetch,我目前正在为我的discord机器人开发计算器功能。我发出了一个命令,获取steam市场项目价格,然后根据以下公式计算:([price]-[price*0.15])*案例数量,其中0.15是市场费用。这就是问题所在 程序将json.lovel_price视为一个单词,而不是一个数字(我想)。因此,bot会发送一条带有NaN的消息。我不知道如何让我的代码正确地将JSON视为一个数字 这是我的密码: const Discord = require('discord.js'); const fetch
([price]-[price*0.15])*案例数量
,其中0.15是市场费用。这就是问题所在
程序将json.lovel_price
视为一个单词,而不是一个数字(我想)。因此,bot会发送一条带有NaN
的消息。我不知道如何让我的代码正确地将JSON视为一个数字
这是我的密码:
const Discord = require('discord.js');
const fetch = require('node-fetch');
const client = new Discord.Client();
client.login('[TOKEN]');
client.on('ready', () => {
console.log(`Logged in as TEST`);
});
//////////////////////////////////
const prefix = "!";
client.on('message', message =>{
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'calculate') {
if (!args.length){
return message.channel.send(`Invalid argument`);
} else if (args[0] === 'breakout'){
fetch(
'https://steamcommunity.com/market/priceoverview/?appid=730&market_hash_name=Operation%20Breakout%20Weapon%20Case¤cy=6',
)
.then((res) => res.json())
.then((json) =>
message.channel.send(
`From ${args[1]] breakout cases you will get ${((json.lowest_price)-((json.lowest_price)*0.15))*(args[1])}`,
),
)
.catch((error) => {
console.log(error);
message.channel.send('tracking breakout price failed');
});
}
}
});
响应包括
最低价格作为字符串(如“7,89z”
)。API似乎在最低价格
字段中包含格式化货币,这就是为什么在使用它时会收到NaN
您可以通过删除货币符号、删除点并用点替换逗号来手动将其转换为数字:
函数getNumberFromCurrency(货币){
返回号码(
通货
.替换(/[zł.]/g')
.替换(“,”,“.”)
)
}
常量金额='10';
const lowestPrice=“7,89zł”;
const lowestPriceValue=getNumberFromCurrency(最低价格);
常数最终价格=(最低价格-最低价格*0.15)*金额;
log({lowestPriceValue,finalPrice});
//=>7.89,67.065