将 json 拆分为单独的对象

Car*_*sel 2 json node.js

我正在使用 node.js 从 JSON 中的外部 API 接收数百个数据对象,如下所示:

[
{
"ID": "6548532",
"Status": "active",
"Updated": "2014-11-24T07:32:04-07:00",
"created": "2014-09-15T19:42:37-07:00",
"URL": "www.example.com",
"Categories": [
  "cat-a",
  "cat-b"
],
"Price": "10.00"
},
{
"ID": "8558455",
"Status": "inactive",
"Updated": "2014-10-24T07:32:04-07:00",
"created": "2014-09-15T19:42:37-07:00",
"URL": "www.example.com",
"Categories": [
  "cat-c",
  "cat-r"
],
"Price": "20.00"
}
....
]
Run Code Online (Sandbox Code Playgroud)

我想将对象分开,以便我只能写入必须写入"Status": "active"数据库的对象。我知道我可以在使用之前使用字符串操作来做到这一点,JSON.parse但我想知道是否有更好的方法将 JSON 文件拆分为它包含的对象并将它们保留在一个数组中,然后我可以处理。

Sha*_*ush 5

将 JSON 解析为 Javascript 对象后,您可以使用filter函数删除"Status"不等于的元素"active"

var responseArray = JSON.parse(responseData),
    filteredArray = responseArray.filter(
       function (obj) {
          return obj.Status == "active";
       });

// Use filteredArray
Run Code Online (Sandbox Code Playgroud)