javascript - Read lines into JSON object -
i using node.js , want read lines contain "=" json object , output json object.
this code reads lines , works correctly:
var lazy = require("lazy"), fs = require("fs"); new lazy(fs.createreadstream('sysctl.conf')).lines.foreach(function(line){ if(line.tostring().indexof("=") > -1) { sysarray.push(line); console.log("contains =" + line); } else { // console.log("not contain = " + line) } }); i'm pushing items array, think due time delay array appears have no objects in when run code:
var arraylength = sysarray.length; console.log("array length:" + arraylength) (var = 0; < arraylength; i++) { console.log(sysarray[i]); } essentially create json object similar this:
where sample line: net.ipv4.conf.default.rp_filter=1
{ “sysctl”: { “net.ipv4.conf.default.rp_filter”: “1”, “morevalues”: “morevalues” } } thanks in advance
you may use split :
var myobj = {}; new lazy(fs.createreadstream('sysctl.conf')).lines.foreach(function(line) { var tmp = line.split('='); // if line = "my.conf=1" tmp = ["my.conf", "1"]; myobj[tmp[0]] = tmp[1]; }); console.log(json.stringify(myobj));
Comments
Post a Comment