简体   繁体   English

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

[英]Return length of a parsed json array

I have an array of objects that i get from a json file. 我有一个从json文件中获取的对象数组。 After getting the array with fs.readFileSync. 用fs.readFileSync获取数组之后。

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

and when I do: 当我这样做时:

console.log(parsedJsonData);

it returns: 710, instead of what i expect to be 1 它返回:710,而不是我期望的是1

here is the array(with only one object) 这是数组(只有一个对象)

[
{
    "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
}
]

I simply want to find the amount of objects that there are in the array, which is 1 so I can loop through it 我只是想找到数组中存在的对象数量,即1,因此我可以遍历它

The unnecessary JSON.stringify() over a string is causing problems, look at this: 字符串上不必要的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; } 

Is returning a escaped string, so when you call the function JSON.parse() this function actually is returning a string: 返回一个转义的字符串,因此当您调用函数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; } 

The solution is removing the call of JSON.stringify 解决方案是删除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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM