如何使用JSON.parse()解析多个对象

时间:2019-07-08 07:18:43

标签: javascript node.js

let string = "{
    "path": "transition_26_1_0 - Copy (2) - Copy - Copy - Copy.xml",
    "fullPath": "D:\\fileuploader\\TEST_2\\transition_26_1_0 - Copy (2) - Copy - Copy - Copy.xml",
    "basename": "transition_26_1_0 - Copy (2) - Copy - Copy - Copy.xml",
    "stats": {
        "dev": 3356173649,
        "mode": 33206,
        "nlink": 1,
        "uid": 0,
        "gid": 0,
        "rdev": 0,
        "ino": 1125899908018589,
        "size": 604,
        "atimeMs": 1560493097203.9585,
        "mtimeMs": 1510127196336.229,
        "ctimeMs": 1560493097204.9585,
        "birthtimeMs": 1560493097203.9585,
        "atime": "2019-06-14T06:18:17.204Z",
        "mtime": "2017-11-08T07:46:36.336Z",
        "ctime": "2019-06-14T06:18:17.205Z",
        "birthtime": "2019-06-14T06:18:17.204Z"
    }
}
{
    "path": "transition_26_1_0 - Copy (2) - Copy - Copy.xml",
    "fullPath": "D:\\fileuploader\\TEST_2\\transition_26_1_0 - Copy (2) - Copy - Copy.xml",
    "basename": "transition_26_1_0 - Copy (2) - Copy - Copy.xml",
    "stats": {
        "dev": 3356173649,
        "mode": 33206,
        "nlink": 1,
        "uid": 0,
        "gid": 0,
        "rdev": 0,
        "ino": 1688849861395786,
        "size": 604,
        "atimeMs": 1560493067976.2869,
        "mtimeMs": 1510127196336.229,
        "ctimeMs": 1560493067977.2869,
        "birthtimeMs": 1560493067976.2869,
        "atime": "2019-06-14T06:17:47.976Z",
        "mtime": "2017-11-08T07:46:36.336Z",
        "ctime": "2019-06-14T06:17:47.977Z",
        "birthtime": "2019-06-14T06:17:47.976Z"
    }
}"

console.log(JSON.parse(string))

我的字符串看起来像这样,我必须转换为对象数组,我尝试使用 JSON.parse()螺母在这样的行上抛出意外的错误“ {”。

注释/更新::我通过管道传输对象流(使用Tranform转换为字符串类型),并通过管道传输至子进程中的process.stdout。

在父级中,我获得了child.stdout.on(“ data”)上的字符串

如果我只有一个对象,则没有错误,但这是错误的。 如何将其转换为对象数组?

更新:在Child.js中

var jsonStream = through2.obj(function(chunk, encoding, callback) {
let details = JSON.stringify(chunk, null, 4) + ',' + '\n'
this.push(details)
callback()
})

files.pipe(jsonStream).pipe(process.stdout);

3 个答案:

答案 0 :(得分:1)

您可以使用站点“ https://codebeautify.org/jsonviewer”来了解您的json字符串是否正确。 这是错误的,因为您可以使用数组或对象,并且字符串不是两者之一。

答案 1 :(得分:0)

以下内容将解析。

您不能包含带有\ n的字符串,即使您没有,它也不是有效的JSON。您需要使用[]换行,在对象之间使用逗号,并转义反斜杠。

在这种情况下,您需要\\\\来转义反斜杠

let string = `[{
        "path": "transition_26_1_0 - Copy (2) - Copy - Copy - Copy.xml",
        "fullPath": "D:\\\\fileuploader\\\\TEST_2\\\\transition_26_1_0 - Copy (2) - Copy - Copy - Copy.xml",
        "basename": "transition_26_1_0 - Copy (2) - Copy - Copy - Copy.xml",
        "stats": {
            "dev": 3356173649,
            "mode": 33206,
            "nlink": 1,
            "uid": 0,
            "gid": 0,
            "rdev": 0,
            "ino": 1125899908018589,
            "size": 604,
            "atimeMs": 1560493097203.9585,
            "mtimeMs": 1510127196336.229,
            "ctimeMs": 1560493097204.9585,
            "birthtimeMs": 1560493097203.9585,
            "atime": "2019-06-14T06:18:17.204Z",
            "mtime": "2017-11-08T07:46:36.336Z",
            "ctime": "2019-06-14T06:18:17.205Z",
            "birthtime": "2019-06-14T06:18:17.204Z"
        }
    },
    {
        "path": "transition_26_1_0 - Copy (2) - Copy - Copy.xml",
        "fullPath": "D:\\\\fileuploader\\\\TEST_2\\\\transition_26_1_0 - Copy (2) - Copy - Copy.xml",
        "basename": "transition_26_1_0 - Copy (2) - Copy - Copy.xml",
        "stats": {
            "dev": 3356173649,
            "mode": 33206,
            "nlink": 1,
            "uid": 0,
            "gid": 0,
            "rdev": 0,
            "ino": 1688849861395786,
            "size": 604,
            "atimeMs": 1560493067976.2869,
            "mtimeMs": 1510127196336.229,
            "ctimeMs": 1560493067977.2869,
            "birthtimeMs": 1560493067976.2869,
            "atime": "2019-06-14T06:17:47.976Z",
            "mtime": "2017-11-08T07:46:36.336Z",
            "ctime": "2019-06-14T06:17:47.977Z",
            "birthtime": "2019-06-14T06:17:47.976Z"
        }
    }]`

console.log(JSON.parse(string))

答案 2 :(得分:0)

以这种方式写

JSON.parse('
  [
    {
      "key": "value", 
      "key": "value"
    },
    {
      "key": "value"
    }
  ]
')

OR

JSON.parse('
  {
    "object": 
    {
      "key": "value", 
      "key": "value"
    },
    "object": 
    {
      "key": "value"
    }
  }
')