0

如何使用 java script 获取按行划分的 json 文件。就像在 python 中使用 readlines() 完成的那样。我上传了一个 .json 文件,我需要在其中找到某些关键字,并显示找到这些关键字的行及其行号。json文件的示例如下:

{
    "data": [],
    "numFound": 0,
    "context": {
        "rows": "50",
        "from": "2014-01-11T17:48:40.000Z",
        "until": "2014-01-11T17:48:40.000Z",
        "start": 0,
        "query": "( json.system_version:3.4.42 json.id.discoveryServerUri:\"svcs.myharmony.com\" json.event_level:error json.crashcode:syncAbort )",
        "order": "desc"
    }

 }
4

2 回答 2

1

我猜你想一次加载一个 json 文件。你可以这样做:

    function loadJSON(callback) {   

              var xobj = new XMLHttpRequest();
              xobj.overrideMimeType("application/json");
              //if you want it synchronous, replace with false
              xobj.open('GET', 'json_file.json', true);
              xobj.onreadystatechange = function () {
              if (xobj.readyState == 4 && xobj.status == "200") {
                callback(xobj.responseText);
              }
        };
        xobj.send(null);  
     }

接着,

readJSON(function(response) {
    // "read" the json
    var data = JSON.parse(response);
   //do whatever you want
 });

希望能帮助到你。

于 2014-03-10T13:01:34.030 回答
0
var raw =  // your json file contents here
var lines = raw.replace("\r\n", "\n").split("\n")
于 2014-03-10T12:52:18.080 回答