Nodejs json文件写入

时间:2017-05-07 15:26:10

标签: javascript json node.js web

我正在制作一个程序,它读取包含数百个链接的数组,读取每个链接并擦除一些文本,然后将其作为json写入输出文件。

我在格式化json文件时遇到问题,因为public function ByName($Name) { $Vendors = Vendors::where('Name', 'like', '%'.$Name.'%') ->with('venues')->paginate(30); return $Vendors; } public function ByType($Type) { $Types = explode('-', $Type); $Vendors = Vendors::whereIn('Type', $Types) ->with('venues')->paginate(30); return $Vendors; } public function ByArea($Area) { $Vendors = Vendors::where('Area', 'like', '%'.$Area.'%') ->with('venues')->paginate(30); return $Vendors; } public function ByRating($Rating) { $Ratings = explode('-', $Rating); $Vendors = Vendors::whereBetween('Rating', $Ratings) ->with('venues')->paginate(30); return $Vendors; } public function ByReviews($Review) { $Reviews = explode('-', $Review); $Vendors = Vendors::whereBetween('Reviews', $Reviews) ->with('venues')->paginate(30); return $Vendors; } public function ByNameType($Name, $Type) { $Types = explode('-', $Type); $Vendors = Vendors::where('Name', 'like', '%'.$Name.'%') ->whereIn('Type', $Types) ->with('venues')->paginate(30); return $Vendors; } public function ByNameArea($Name, $Area) { $Vendors = Vendors::where('Name', 'like', '%'.$Name.'%') ->where('Area', 'like', '%'.$Area.'%') ->with('venues')->paginate(30); return $Vendors; } public function ByTypeArea($Type, $Area) { $Types = explode('-', $Type); $Vendors = Vendors::where('Area', 'like', '%'.$Area.'%') ->whereIn('Type', $Types) ->with('venues')->paginate(30); return $Vendors; } public function ByNameTypeArea($Name, $Type, $Area) { $Types = explode('-', $Type); $Vendors = Vendors::where('Name', 'like', '%'.$Name.'%') ->where('Area', 'like', '%'.$Area.'%') ->whereIn('Type', $Types) ->with('venues')->paginate(30); return $Vendors; } 会像这样返回json对象:

.map()

而不是像这样的json数组:

{
   "id": "wajnh3ivnydeegrr",
   "lorem ipsum"
},{
   "id": "6yuyz57cmrgo5fbe",
   "message": "lorem ipsum"
},

如何在格式正确的情况下将每个json对象推入数组? (不知何故,必须排除最后一个尾随逗号)。真的很感谢你的帮助!完整的请求如下。完整代码可在Github here中找到。

[{
   "id": "wajnh3ivnydeegrr",
   "lorem ipsum"
},{
   "id": "6yuyz57cmrgo5fbe",
   "message": "lorem ipsum"
}]

1 个答案:

答案 0 :(得分:1)

您需要订购您的请求并了解您的第一个和最后一个请求,以便我们可以使用promises或名为async的简单nodejs库,有一个名为async.series的函数并使用Array.prototype.map()来构建一个名为tasks的数组,它将具有一系列功能。 所有函数都将按照dataDocument数组的相同顺序执行。

安装async

npm install --save async 

然后使用以下修改后的代码

var async = require('async');

app.get('/scrape', function(req, res) { 
    var tasks = dataDocument.map(function(item, index) { // create a series of functions to be executed in order 
        return function(cb){
            request(item, function(err, response, html) {
                if (!err) {
                    //Variables
                    var message, id, date , string ;
                    var $ = cheerio.load(html);
                    message = $(".messagebody").text().trim();
                    //Get Id
                    item = item.split('/');
                    id = item[4];
                    //Make json entry
                    var json = {
                        "id": id,
                        "message": message,
                        "date": date
                    };
                    string = JSON.stringify(json, null, 4, function(json) {
                        console.log('Successfully written ' + json.id);
                    });
                    var output = string + ',';
                    if(index === 0){
                        output = '[' + string + ','; // first item
                    }
                    if(index === dataDocument.length - 1 ){
                        output = string + ']'; // last item
                    }
                    fs.appendFile('./output.json', output ,cb);
                }
            });
        }; 
    });
    async.series(tasks,function(){
        console.log('Successfully written all documents!');
    })
});
相关问题