簡體   English   中英

在nodeJS中創建Json文件

[英]Json File Creation in nodeJS

我需要在nodeJS中以以下格式創建JSON文件,也需要遍歷到特定位置並添加值(如果有)。 我已經用Java完成了,但是在NodeJs中卻一無所知。 如果有人幫助我會很有幫助。 謝謝。

JSON檔案格式

如果我的理解是正確的,那么您正在使用Protractor自動化AngularJS測試,並將結果寫為JSON,以便報告/一旦完成就解析回去? 如果是這樣,您可以簡單地將結果存儲為Java中的對象,然后使用fs節點包將其寫出。

編寫JSON報告文件

var fs = require('fs');
//..Protractor .. and other logic..
var results = { Project : "xxx" , ....};
//...more of that assignment....
//You could use JSON.parse() or JSON.stringify() to read/convert this object to string vice versa and fs package to read/write it to file.

fs.writeFile("/tmp/testreport.json", JSON.stringify(results), function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The test report file was saved as JSON file!");
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM