简体   繁体   English

将 JSON 从 Firebase 解析为 Z9E13B69D1D2DA927102ACAAAF7154A37 中的 object

[英]Parse JSON from Firebase to object in Javascript

I exported the database from Firebase in Json format, and It looks weird but it's legit JSON syntax.我从 Firebase 以 Json 格式导出了数据库,它看起来很奇怪,但它是合法的 JSON 语法。 Does anyone know how to parse this into a list of objects?有谁知道如何将其解析为对象列表?

JSON code: JSON 代码:

{
  "users" : 
  {
    "GSIgfyiEGtZs5reYe4SpwFJVxDC2" :
    {
      "email" : "anic@hotmail.com",
      "password" : "Dava123",
      "score" : 0,
      "username" : "Anic2",
      "zdate" : "2-3"
    },
    "OHxA5ARnbYdsy9Ga1nxDy0gZQBv1" : {
      "email" : "dava@hotmail.com",
      "password" : "Dava123",
      "score" : 3,
      "username" : "dava",
      "zdate" : "01-03-2021  11:53"
    }
  }
}

If you are trying to access the values, you can use the keys attribute如果您尝试访问这些值,则可以使用 keys 属性

var myFireBaseObj = {
  "users" : 
  {
    "GSIgfyiEGtZs5reYe4SpwFJVxDC2" :
    {
      "email" : "anic@hotmail.com",
      "password" : "Dava123",
      "score" : 0,
      "username" : "Anic2",
      "zdate" : "2-3"
    },
    "OHxA5ARnbYdsy9Ga1nxDy0gZQBv1" : {
      "email" : "dava@hotmail.com",
      "password" : "Dava123",
      "score" : 3,
      "username" : "dava",
      "zdate" : "01-03-2021  11:53"
    }
  }
};

console.log( Object.keys(myFireBaseObj));

// if you want to get both values and keys / users etc seperately as objects
var keys = Object.keys(myFireBaseObj );
var values = Object.values(myFireBaseObj );

console.log(keys[0]);
// put your attribute you want here
console.log(values [0]['...']); 

Option 2: if you want an array back, use map for eg adapt it below选项 2:如果您想要返回一个阵列,请使用map例如在下面进行调整

 Object.keys(myFireBaseObj).map(function(a,i){
 // whatever you want in the array you map it here
 // you have to customize the nested levels
 return
 [i,myFireBaseObj[a].blahBlah.. ,
 myFireBaseObj[a].password] 
 })

Update : Added sample to show for loop to get the key , values更新:添加示例以显示for loop以获取keyvalues

for (var key of Object.keys(myFireBaseObj)) {
    // this will give you the key & values for all properties
    console.log(key + " -> " + p[key])
    // .. process the data here! 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM