我希望能够从一个大的json文件中更新用d3生成的图表上的数据和可视化。这些数据来自美国农业部的营养数据库。json文件来源于这里:http://ashleyw.co.uk/project/food-nutrient-database,它非常大(特别是30 is )。只是装货是个麻烦。Notepad++将其全部加载到(1)行(大约一分钟后),记事本(约20秒)以糟糕的格式加载它(跨越多行)。能否有效地使用这么大的Json数据库?它会使浏览器崩溃或导致某种装载滞后吗?
发布于 2013-10-15 09:25:06
如前所述,我的建议是对JSON进行预处理,以删除不需要的任何内容。下面是Node.js中的一个示例脚本,它将读取正在使用的文件,并输出一个具有大量内容过滤的新文件。
在这个例子中,除了描述之外,我忽略了所有的领域,只包括有关维生素的信息。根数组中仍然应该有6600个元素。
此脚本的结果文件约为5mb,而不是30 5mb。
var fs = require('fs');
// open the file
fs.readFile('./foods-2011-10-03.json','utf8',function(err,data){
if (err) throw err;
var output = [];
// parse the file from a string into an object
data = JSON.parse(data);
// Loop through each element
data.forEach(function(d,i){
// spit an example element into the console for inspection:
// if ( i == 0 ) console.log(d);
// decide which parts of the object you'd like to keep
var element = {
description : d.description,
nutrients : []
};
// for example here I'm just keeping vitamins
d.nutrients.forEach(function(d,i){
if ( d.description.indexOf("Vitamin") == 0 ) element.nutrients.push(d);
});
output.push(element);
})
fs.writeFile( './foods-output.json', JSON.stringify(output), function(err){
if ( err ) throw err;
console.log('ok');
})
})
https://stackoverflow.com/questions/19376656
复制相似问题