返回已解析的json数组的长度

时间:2018-11-02 02:28:31

标签: javascript arrays json object bson

我有一个从json文件获得的对象数组。使用fs.readFileSync获取阵列之后。

jsonData = JSON.stringify(fs.readFileSync('data.json'.toString(), 'utf8'));
parsedJsonData = JSON.parse(jsonData);

当我这样做时:

console.log(parsedJsonData);

它返回:710,而不是我期望的1

这是数组(只有一个对象)

[
{
    "email": "ibrahim.m.fadel@gmail.com",
    "username": "ibrahim fadel",
    "password": {
        "type": "Buffer",
        "data": [
            25,
            0,
            0,
            0,
            2,
            115,
            116,
            114,
            105,
            110,
            103,
            0,
            8,
            0,
            0,
            0,
            99,
            97,
            114,
            101,
            121,
            51,
            49,
            0,
            0
        ]
    },
    "id": 0
}
]

我只想查找数组中存在的对象数量,即1,这样我就可以遍历它

1 个答案:

答案 0 :(得分:2)

字符串上不必要的JSON.stringify()会引起问题,请查看以下内容:

console.log(JSON.stringify("[\n" +    "{\n" +    "    \"email\": \"ibrahim.m.fadel@gmail.com\",\n" +    "    \"username\": \"ibrahim fadel\",\n" +    "    \"password\": {\n" +    "        \"type\": \"Buffer\",\n" +    "        \"data\": [\n" +    "            25,\n" +    "            0,\n" +    "            0,\n" +    "            0,\n" +    "            2,\n" +    "            115,\n" +    "            116,\n" +    "            114,\n" +    "            105,\n" +    "            110,\n" +    "            103,\n" +    "            0,\n" +    "            8,\n" +    "            0,\n" +    "            0,\n" +    "            0,\n" +    "            99,\n" +    "            97,\n" +    "            114,\n" +    "            101,\n" +    "            121,\n" +    "            51,\n" +    "            49,\n" +    "            0,\n" +    "            0\n" +    "        ]\n" +    "    },\n" +    "    \"id\": 0\n" +    "}\n" +    "]"))
.as-console-wrapper { max-height: 100% !important; top: 0; }

正在返回转义的字符串,因此,当您调用函数JSON.parse()时,该函数实际上是在返回字符串:

console.log(typeof JSON.parse(JSON.stringify("[\n" +    "{\n" +    "    \"email\": \"ibrahim.m.fadel@gmail.com\",\n" +    "    \"username\": \"ibrahim fadel\",\n" +    "    \"password\": {\n" +    "        \"type\": \"Buffer\",\n" +    "        \"data\": [\n" +    "            25,\n" +    "            0,\n" +    "            0,\n" +    "            0,\n" +    "            2,\n" +    "            115,\n" +    "            116,\n" +    "            114,\n" +    "            105,\n" +    "            110,\n" +    "            103,\n" +    "            0,\n" +    "            8,\n" +    "            0,\n" +    "            0,\n" +    "            0,\n" +    "            99,\n" +    "            97,\n" +    "            114,\n" +    "            101,\n" +    "            121,\n" +    "            51,\n" +    "            49,\n" +    "            0,\n" +    "            0\n" +    "        ]\n" +    "    },\n" +    "    \"id\": 0\n" +    "}\n" +    "]")))
.as-console-wrapper { max-height: 100% !important; top: 0; }

解决方案是删除呼叫JSON.stringify

console.log(JSON.parse("[\n" +    "{\n" +    "    \"email\": \"ibrahim.m.fadel@gmail.com\",\n" +    "    \"username\": \"ibrahim fadel\",\n" +    "    \"password\": {\n" +    "        \"type\": \"Buffer\",\n" +    "        \"data\": [\n" +    "            25,\n" +    "            0,\n" +    "            0,\n" +    "            0,\n" +    "            2,\n" +    "            115,\n" +    "            116,\n" +    "            114,\n" +    "            105,\n" +    "            110,\n" +    "            103,\n" +    "            0,\n" +    "            8,\n" +    "            0,\n" +    "            0,\n" +    "            0,\n" +    "            99,\n" +    "            97,\n" +    "            114,\n" +    "            101,\n" +    "            121,\n" +    "            51,\n" +    "            49,\n" +    "            0,\n" +    "            0\n" +    "        ]\n" +    "    },\n" +    "    \"id\": 0\n" +    "}\n" +    "]")
.length)
.as-console-wrapper { max-height: 100% !important; top: 0; }