gpt4 book ai didi

javascript - 返回解析后的 json 数组的长度

转载 作者:行者123 更新时间:2023-12-01 01:32:52 24 4
gpt4 key购买 nike

我有一个从 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,这样我就可以循环遍历它

最佳答案

字符串上不必要的 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; }

关于javascript - 返回解析后的 json 数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53111931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com