簡體   English   中英

返回已解析的json數組的長度

[英]Return length of a parsed json array

我有一個從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; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM