从Firebase数据库中获取JSON数据的价值

时间:2017-08-08 04:10:57

标签: javascript json firebase firebase-realtime-database

我从Firebase实时数据库收到JSON数据 -

这是Json:

var data={
"07:08:2017_16:57:48": {
"fech": "07:08:2017",
"hor": "16:57:48",
"lat": -41.1515438,
"lng": -71.4083591,
"telef": ""
},
"07:08:2017_16:58:04": {
"fech": "07:08:2017",
"hor": "16:58:04",
"lat": -41.1515438,
"lng": -71.4083591,
"telef": "2944895972"
},
"07:08:2017_16:58:05": {
"fech": "07:08:2017",
"hor": "16:58:05",
"lat": -41.1515438,
"lng": -71.4083591,
"telef": "2944895972"
 }
 }

我需要lat值,但我不明白如何在这种情况下使用索引

我试过了:

var dataj= JSON.parse(data);
var latitud1= dataj[1]; 

任何帮助?

1 个答案:

答案 0 :(得分:1)

如果您确实拥有该JavaScript结构,则此代码将打印纬度值:

Object.keys(data).forEach(function(key) {
  console.log(data[key].lat);
});

Runnable片段:

var data = {
    "07:08:2017_16:57:48": {
        "fech": "07:08:2017",
        "hor": "16:57:48",
        "lat": -41.1515438,
        "lng": -71.4083591,
        "telef": ""
    },
    "07:08:2017_16:58:04": {
        "fech": "07:08:2017",
        "hor": "16:58:04",
        "lat": -41.1515438,
        "lng": -71.4083591,
        "telef": "2944895972"
    },
    "07:08:2017_16:58:05": {
        "fech": "07:08:2017",
        "hor": "16:58:05",
        "lat": -41.1515438,
        "lng": -71.4083591,
        "telef": "2944895972"
    }
};
Object.keys(data).forEach(function(key) {
  console.log(data[key].lat);
});