2017-06-21 454 views
3

以下是我的JSON。我想在這個對象中獲得數組和數組的名稱。這是動態創建的,所以我不知道它的數量和名稱。這裏是這個例子中的2個數組,名爲Table和Table1。如何獲取JSON對象中的數組數量

"{ 
     "Table": [ 
     { 
      "Day": "Jan", 
      "Counts": 20, 
      "SrNo": 1, 
      "Title": "test2", 
      "ProfilePic": "/Image1.jpg" 
     }, 
     { 
      "Day": "Feb", 
      "Counts": 10, 
      "SrNo": 2,   
      "Title": "test2", 
      "ProfilePic": "/Image1.jpg"   
     } 
    ], 
"Table1": [ 
    { 
     "Day": "01", 
     "Counts": 5, 
     "SrNo": 1,  
     "Title": "test3", 
     "ProfilePic": "/Image2.jpg" 
    }, 
    { 
     "Day": "02", 
     "Counts": 9, 
     "SrNo": 2,   
     "Title": "test3", 
     "ProfilePic": "/Image2.jpg",   
    } 
    ] 
}" 
+0

能夠保證所有的陣列將只對第一個「級別」,並不是嵌套對象? – Phillip

+0

感謝您的答案,我的情況我需要json.stringify即,Object.keys(JSON.parse(obj))。length; –

+0

[獲取Json對象上的項目總數?](https://stackoverflow.com/questions/13782698/get-total-number-of-items-on-json-object) –

回答

10

嘗試以下提到的代碼,

Object.keys(jsonObject).length; 

另請參閱...:Get total number of items on Json object?

要獲得所有的名字:

var keys = Object.keys(jsonObject); // this will return root level title ["Table" , "Table1"] 
+0

我們可以得到名字嗎? –

+0

要獲取名稱,只需刪除'.length'部分。你現在將有一個鍵的數組:) – Phillip

+0

謝謝@Phillip .. –

4

假設每屬性在對象包含數組,你可以指望使用Object.keys屬性的數量,就像這樣:

var arrayCount = Object.keys(obj).length; 

或者,如果你真的想確定屬性的類型,如果有一些其他類型的對象時,你會需要遍歷並逐個檢查每個屬性,這可以使用filter()這樣進行:

var obj = { 
 
    "Table": [{ 
 
     "Day": "Jan", 
 
     "Counts": 20, 
 
     "SrNo": 1, 
 
     "Title": "test2", 
 
     "ProfilePic": "/Image1.jpg" 
 
    }, 
 
    { 
 
     "Day": "Feb", 
 
     "Counts": 10, 
 
     "SrNo": 2, 
 
     "Title": "test2", 
 
     "ProfilePic": "/Image1.jpg" 
 
    } 
 
    ], 
 
    "Table1": [{ 
 
     "Day": "01", 
 
     "Counts": 5, 
 
     "SrNo": 1, 
 
     "Title": "test3", 
 
     "ProfilePic": "/Image2.jpg" 
 
    }, 
 
    { 
 
     "Day": "02", 
 
     "Counts": 9, 
 
     "SrNo": 2, 
 
     "Title": "test3", 
 
     "ProfilePic": "/Image2.jpg", 
 
    } 
 
    ], 
 
    'NotArray1': 'foo', // < not an array 
 
    'isArray': false // < not an array 
 
} 
 

 
var arrayCount = Object.keys(obj).filter(function(key) { 
 
    return obj[key].constructor === Array; 
 
}).length; 
 
console.log(arrayCount);

+0

謝謝你的幫助@Rory .. –

1

呦ü可以使用Array.prototype.reduce(),以總回報均是有效的數組對象的屬性值:

var obj = { 
 
    "Table": [{ 
 
     "Day": "Jan", 
 
     "Counts": 20, 
 
     "SrNo": 1, 
 
     "Title": "test2", 
 
     "ProfilePic": "/Image1.jpg" 
 
    }, { 
 
     "Day": "Feb", 
 
     "Counts": 10, 
 
     "SrNo": 2, 
 
     "Title": "test2", 
 
     "ProfilePic": "/Image1.jpg" 
 
    }], 
 
    "Table1": [{ 
 
     "Day": "01", 
 
     "Counts": 5, 
 
     "SrNo": 1, 
 
     "Title": "test3", 
 
     "ProfilePic": "/Image2.jpg" 
 
     }, { 
 
     "Day": "02", 
 
     "Counts": 9, 
 
     "SrNo": 2, 
 
     "Title": "test3", 
 
     "ProfilePic": "/Image2.jpg" 
 
     } 
 
    ], 
 
    "Table2": false 
 
    }, 
 
    arrayCount = Object.keys(obj).reduce(function (acc, val) { 
 
    return Array.isArray(obj[val]) ? ++acc : acc; 
 
    }, 0); 
 

 
console.log(arrayCount);

+0

謝謝@Yosvel .. –