简体   繁体   English

如何在Node.js中使用JSON数据?

[英]How to use JSON data in Node.js?

I have a file, called ETHBTC.json : 我有一个名为ETHBTC.json的文件:

 [{
    "open": "0.06252000",
    "high": "0.06264700",
    "low": "0.06239800",
    "close": "0.06254100",
    "volume": "681.69300000",
    "timestamp": 1521575400000
},
{
    "open": "0.06253500",
    "high": "0.06270000",
    "low": "0.06242800",
    "close": "0.06261900",
    "volume": "371.99900000",
    "timestamp": 1521575700000
},
{
    "open": "0.06261500",
    "high": "0.06280000",
    "low": "0.06257500",
    "close": "0.06266200",
    "volume": "519.11000000",
    "timestamp": 1521576000000
},
...
]

I am trying to save the low value to a variable in Node.js so I can add all the low values together, etc: 我试图将low保存到Node.js中的变量中,以便可以将所有低值加在一起,等等:

for(item in words) {
   var lowTotal = 0;
   lowTotal += words.low;
}

But I have no luck. 但是我没有运气。

I'm also having trouble with the console.log just to log the low variable. 我也遇到了console.log问题,只是为了记录low变量。

First you need to parse the JSON file: 首先,您需要解析JSON文件:

let fs = require('fs');
let content = fs.readFileSync('PathToFile').toString();

Then you need to parse it: 然后,您需要解析它:

let jsonData = JSON.parse(content);

Then iterate over the elements. 然后遍历元素。 I recommend the for...of loop 我推荐for ... of循环

let total = 0;
for(let element of jsonData){
    total += element.low
}

You can also use Array.prototype.map or Array.prototype.reduce but first stick to the basics. 您也可以使用Array.prototype.mapArray.prototype.reduce但首先要坚持基础。

Also please be sure to work on the right types: 另外,请确保使用正确的类型:

your numbers in the JSON are saved as strings. 您在JSON中的数字将另存为字符串。 You will have to convert them also: 您还必须转换它们:

let total = 0;
for(let element of jsonData){
    total += parseFloat(element.low);
}

please use Object.values like below. 请使用如下所示的Object.values。 Convert JSON to object though using 'JSON.Parse' method 使用'JSON.Parse'方法将JSON转换为对象

let sum = 0;
Object.values(YOURJSONOBJECT).forEach(element => {

sum += parseFloat(element["low"]);

});

console.log(sum);

and result would be "0.18740099999999998", which is the sum of 'low' property 结果将是“ 0.18740099999999998”,即“低”属性的总和

Just iterate over the object and sum the total 只需遍历对象并求和即可

 var data = [{ "open": "0.06252000", "high": "0.06264700", "low": "0.06239800", "close": "0.06254100", "volume": "681.69300000", "timestamp": 1521575400000 }, { "open": "0.06253500", "high": "0.06270000", "low": "0.06242800", "close": "0.06261900", "volume": "371.99900000", "timestamp": 1521575700000 }, { "open": "0.06261500", "high": "0.06280000", "low": "0.06257500", "close": "0.06266200", "volume": "519.11000000", "timestamp": 1521576000000 }] var lowTotal = 0; data.map(a=>lowTotal+=+a.low) console.log(lowTotal) 

You can reduce to give an initial value & increment per item in the array 您可以减少以给出数组中每个项目的初始值和增量

const json = JSON.parse('...');
const lowTotal = json.reduce((val, item) => val += parseFloat(item.low), 0);

Example

 const data = `[{ "open": "0.06252000", "high": "0.06264700", "low": "0.06239800", "close": "0.06254100", "volume": "681.69300000", "timestamp": 1521575400000 }, { "open": "0.06253500", "high": "0.06270000", "low": "0.06242800", "close": "0.06261900", "volume": "371.99900000", "timestamp": 1521575700000 }, { "open": "0.06261500", "high": "0.06280000", "low": "0.06257500", "close": "0.06266200", "volume": "519.11000000", "timestamp": 1521576000000 }]`; const json = JSON.parse(data); const lowTotal = json.reduce((val, item) => val += parseFloat(item.low), 0); console.log(`Total low=${lowTotal}`); 

I am not sure if you are having trouble reading json file but there are bugs in your code 我不确定您是否无法读取json文件,但是代码中存在错误

var lowTotal = 0; should be outside for loop. 应该在循环之外。

Parse String JSON object to float 解析String JSON对象以浮动

Read item not words 阅读item而不是words

 var lowTotal = 0;
for(item in words){
    lowTotal += parseFloat(item.low);
 }

You have several issues in your code: 您的代码中有几个问题:

  1. You have var lowTotal = 0; 您有var lowTotal = 0; inside for which is incorrect 里面for这是不正确
  2. Better to use a for loop so that you get each item of array rather than index of array as in for(item in words) 最好使用for循环,以便获得数组的每个项目,而不是像for(item in words)那样获得数组的索引
  3. low is a string type value so convert it to float type and add it to get the sum. lowstring类型的值,因此将其转换为float类型并相加即可得到总和。

  var words = [{ "open": "0.06252000", "high": "0.06264700", "low": "0.06239800", "close": "0.06254100", "volume": "681.69300000", "timestamp": 1521575400000 }, { "open": "0.06253500", "high": "0.06270000", "low": "0.06242800", "close": "0.06261900", "volume": "371.99900000", "timestamp": 1521575700000 }, { "open": "0.06261500", "high": "0.06280000", "low": "0.06257500", "close": "0.06266200", "volume": "519.11000000", "timestamp": 1521576000000 }]; var lowTotal = 0; words.forEach(function(word){ lowTotal += parseFloat(word.low); }); console.log(lowTotal) 

您可以使用reduce()使用单线执行此操作

let sum = words.reduce((a,c)=>a+parseFloat(c.low), 0);

You should fix your code a bit: 您应该修复一下代码:

let lowTotal = 0;
for(item in words){
    lowTotal += item.low;
}

Or you can do it in a bit different way: 或者,您可以通过其他方式来做到这一点:

let lowTotal = 0;
words.forEach(word => lowTotal += word.low);

Or the most elegant way: 或最优雅的方式:

let lowTotal = words.reduce((a, b) => {
    if(isNaN(a)) { a = a.low; }
    return a + b.low;
}

For..in is used to iterate over properties of an object. For..in用于迭代对象的属性。 there are many ways to loop throw an array of object, the easiest is to use for . 有很多方法可以循环抛出对象数组,最简单的方法是使用for one last thing the lowTotal variable should be declared outside the loop: 最后一点,应该在循环外声明lowTotal变量:

 var words= [{ "open": "0.06252000", "high": "0.06264700", "low": "0.06239800", "close": "0.06254100", "volume": "681.69300000", "timestamp": 1521575400000 }, { "open": "0.06253500", "high": "0.06270000", "low": "0.06242800", "close": "0.06261900", "volume": "371.99900000", "timestamp": 1521575700000 }, { "open": "0.06261500", "high": "0.06280000", "low": "0.06257500", "close": "0.06266200", "volume": "519.11000000", "timestamp": 1521576000000 }]; var lowTotal = 0; for(var i=0; i<words.length; i++){ lowTotal += parseFloat(words[i].low); } console.log(lowTotal); 

As some other suggested. 如其他建议。 But brackets around your tickerdata to create an array. 但是将报价数据括起来以创建一个数组。

Another suggestion. 另一个建议。

var tickers = require('./JSONFILE.json')

for (const value of Object.values(tickers)) {

  console.log(value.low)

}

since you probably don't want to sum all low values. 因为您可能不想汇总所有低值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM