计算JSON字符串中的元素数量

时间:2015-08-07 15:46:32

标签: javascript jquery json

我有一个JSON字符串,表示一行的数据表。这是表格:

"MyData":[
    {
        "FirstName":"John",
        "LastName":"Doe",
        "ID":1234,
        "QCR3m":null,
        "FR3m":null,
        "ERI3m":null,
        "QCR6m":null,
        "FR6m":null,
        "ERI6m":null,
        "QCR1y":90.05,
        "FR1y":null,
        "ERI1y":93.55,
        "QCR3y":93.51,
        "FR3y":95.86,
        "ERI3y":94.0,
        "QCR5y":93.25,
        "FR5y":95.86,
        "ERI5y":94.0
    }
]

我想计算MyData中的列数。在这种情况下,数字将为18.这可以在javascript中执行吗?

1 个答案:

答案 0 :(得分:2)

Object.keyslength

一起使用

var obj = {
        "FirstName":"John",
        "LastName":"Doe",
        "ID":1234,
        "QCR3m":null,
        "FR3m":null,
        "ERI3m":null,
        "QCR6m":null,
        "FR6m":null,
        "ERI6m":null,
        "QCR1y":90.05,
        "FR1y":null,
        "ERI1y":93.55,
        "QCR3y":93.51,
        "FR3y":95.86,
        "ERI3y":94.0,
        "QCR5y":93.25,
        "FR5y":95.86,
        "ERI5y":94.0
    };

console.log(Object.keys(obj).length);