是否有可能获得此类JSON元素的数据?

时间:2017-05-31 20:55:26

标签: javascript json

例如,如果我有JSON文件,如:

{
"update" : {
   "email@gmail.com":{
        "1234": {"notfication": "This is testing 1","url": "http://example1.com"},
        "4567": {"notfication": "This is testing 3","url": "http://facebook.com"}
                  },
  "testing@gmail.com":{
        "abcd": {"notfication": "This is testing 1","url": "http://example2.com"},
        "efgh": {"notfication": "This is testing 3","url": "http://facebook.com"}
                  }
}
}

是否可以要求服务器返回文本:“email@gmail.com”和“testing@gmail.com”?

谢谢

1 个答案:

答案 0 :(得分:0)

var obj = {
                        "update" : {
                               "email@gmail.com":{
                                    "1234": {"notfication": "This is testing 1","url": "http://example1.com"},
                                    "4567": {"notfication": "This is testing 3","url": "http://facebook.com"}
                                              },
                              "testing@gmail.com":{
                                    "abcd": {"notfication": "This is testing 1","url": "http://example2.com"},
                                    "efgh": {"notfication": "This is testing 3","url": "http://facebook.com"}
                                              }
                            }
                            };


                var keys = Object.keys(obj.update);
                console.log(keys[0]);
                console.log(keys[1]);

另见Ranga的回答here