在nodejs

时间:2017-10-06 06:12:30

标签: javascript json node.js

var resobj = {
    "status": {
        "code": 2000,
        "message": "Success"
    },
    "order": {
        "Shop": 1,
        "Quantity": 1,
        "Customer": 1 
    }
}

我有这个json,我需要遍历订单并访问密钥(商店,数量,客户)。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

使用Object#键将返回键数组



var resobj = {
    "status": {
        "code": 2000,
        "message": "Success"
    },
    "order": {
        "Shop": 1,
        "Quantity": 1,
        "Customer": 1 
    }
};

Object.keys(resobj.order).forEach(key => console.log(key));




答案 1 :(得分:0)

您正在寻找的是Object.keys [MDN]

示例:

Object.keys(resobj.order).forEach((key) => {
    console.log(key);
});
// Will log the keys "Shop", "Quantity" and "Customer"