如何从节点js中的json单独获取键和值

时间:2016-02-10 10:30:50

标签: json node.js key

我有像这样的json

{"Beauty_Personal_Care": {
"listingVersions": {
  "v1": {
    "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:586:680986904635?expiresAt=1455143400082&signature=gtvuofdhkeqxipadfzyf"
  }
 }
},
"Eyewear": {
"listingVersions": {
  "v1": {
    "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:473:630636448881?expiresAt=1455143400082&signature=gtvuofdhkeqxipadfzyf"
   }
  }
 }
}

我想在节点js后端分别获取键值的键值和对象。

预期结果:

  

名称= “Beauty_Personal_Care”;

     

URL = “http://affiliate-feeds.snapdeal.com/feed/api/category/v1:586:680986904635?expiresAt=1455143400082&signature=gtvuofdhkeqxipadfzyf”;

1 个答案:

答案 0 :(得分:1)

var json = {}; // your json
var result = [];
Object.keys(json).forEach(function (name) {
    var data = {
        name: name
    };

    data.url = json[name].listingVersions.v1.get;
    result.push(data);
});

console.log(result);