从.JSON中抓取一个数字

时间:2016-07-25 16:40:16

标签: json regex node.js

我有一长串数据,格式如下:

[
{
    "ID": "1234",
    "date/time": "2016-07-18 18:21:44",
    "source_address": "8011",
    "lat": "40.585260",
    "lng": "-105.084420",
}
]

我正在创建一个脚本来从每行中提取值。例如,如果一行包含" ID":我希望能够存储值" 1234"变成一个变量,所以我可以用不同的格式存储它。

这是我的代码,用于检测" ID":

'use strict';

let lineReader = require('line-reader');

//.JSON variables from input file
let id;


//begin creating new object
console.log('var source = {');
//output the file
lineReader.eachLine('dataOut.json', function (line, last) {

    //detect ID, and print it out in the new format
    if (id =~ /^id:$/) {
        console.log('id: "') + console.log('",');
    }

    //done
    if (last) {
        console.log('}');
        return false; // stop reading
    }
});

一旦我检测到ID,我就不确定如何获得" ID"之后的值。在那条线上。

在检测到它们在哪一行后,如何将值存储在一行上?

1 个答案:

答案 0 :(得分:3)

除非你的json文件很笨,你可以只需要它,然后它就是一个内存中的JS对象。

var obj = require('./dataOut.json');

// first element
console.log(obj[0]);
相关问题