似乎无法理解 Firebase 的 JSON table
Cannot seem to understand Firebase's JSON table
假设我有这棵 JSON 树:
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
如何在 Firebase 中执行此操作?每次我在 "employees" 下创建一个名称为 "firstname" 的对象时,它都会用 "Firstname".
替换之前的对象
我以前用过Parse的表格,但是既然它被拿下了,那么我需要帮助来学习这个令人困惑的东西。
我正在使用 Android。
您可能正在寻找 DatabaseReference.push()
,它会在该位置下创建一个新的 child。
var employeesRef = mDatabase.child("employees");
var newEmployeeRef = employeesRef.push()
newEmployeeRef.setValue(employee);
阅读更多相关信息的最佳位置是 appending data to a list in the Firebase documentation。
部分
Firebase 数据库本身不支持列表或数组。如果我们尝试存储一个列表或数组,它实际上被存储为一个 "object",以整数作为键名 (see doc)。
// we send this
['hello', 'world']
// Firebase databases store this
{0: 'hello', 1: 'world'}
通过这种方式,您在 firebase 中的树将如下所示:
{"employees":{
0:{"firstName":"John", "lastName":"Doe"},
1:{"firstName":"Anna", "lastName":"Smith"},
2:{"firstName":"Peter","lastName":"Jones"}
}
}
使用 Firebase 术语,我们可以说节点 emloyees 有三个子节点,ID 分别为 0、1、2。
但不建议在 Firebase 中保存具有整数 ID 的数据 (see this to know why)。 Firebase 提供了一个 push()
函数,每次将新的子项添加到指定的 Firebase 引用时,该函数都会生成一个唯一的 ID。
这是来自 Firebase Android 文档的示例:
//create firebase ref using your firebase url
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog");
Firebase postRef = ref.child("posts");
Map<String, String> post1 = new HashMap<String, String>();
post1.put("author", "gracehop");
post1.put("title", "Announcing COBOL, a New Programming Language");
postRef.push().setValue(post1);
Map<String, String> post2 = new HashMap<String, String>();
post2.put("author", "alanisawesome");
post2.put("title", "The Turing Machine");
postRef.push().setValue(post2);
作为 posts 节点的结果,我们将有两个具有自动生成 ID 的子节点:
{
"posts": {
"-JRHTHaIs-jNPLXOQivY": {
"author": "gracehop",
"title": "Announcing COBOL, a New Programming Language"
},
"-JRHTHaKuITFIhnj02kE": {
"author": "alanisawesome",
"title": "The Turing Machine"
}
}
}
假设我有这棵 JSON 树:
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
如何在 Firebase 中执行此操作?每次我在 "employees" 下创建一个名称为 "firstname" 的对象时,它都会用 "Firstname".
替换之前的对象我以前用过Parse的表格,但是既然它被拿下了,那么我需要帮助来学习这个令人困惑的东西。
我正在使用 Android。
您可能正在寻找 DatabaseReference.push()
,它会在该位置下创建一个新的 child。
var employeesRef = mDatabase.child("employees");
var newEmployeeRef = employeesRef.push()
newEmployeeRef.setValue(employee);
阅读更多相关信息的最佳位置是 appending data to a list in the Firebase documentation。
部分Firebase 数据库本身不支持列表或数组。如果我们尝试存储一个列表或数组,它实际上被存储为一个 "object",以整数作为键名 (see doc)。
// we send this
['hello', 'world']
// Firebase databases store this
{0: 'hello', 1: 'world'}
通过这种方式,您在 firebase 中的树将如下所示:
{"employees":{
0:{"firstName":"John", "lastName":"Doe"},
1:{"firstName":"Anna", "lastName":"Smith"},
2:{"firstName":"Peter","lastName":"Jones"}
}
}
使用 Firebase 术语,我们可以说节点 emloyees 有三个子节点,ID 分别为 0、1、2。
但不建议在 Firebase 中保存具有整数 ID 的数据 (see this to know why)。 Firebase 提供了一个 push()
函数,每次将新的子项添加到指定的 Firebase 引用时,该函数都会生成一个唯一的 ID。
这是来自 Firebase Android 文档的示例:
//create firebase ref using your firebase url
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog");
Firebase postRef = ref.child("posts");
Map<String, String> post1 = new HashMap<String, String>();
post1.put("author", "gracehop");
post1.put("title", "Announcing COBOL, a New Programming Language");
postRef.push().setValue(post1);
Map<String, String> post2 = new HashMap<String, String>();
post2.put("author", "alanisawesome");
post2.put("title", "The Turing Machine");
postRef.push().setValue(post2);
作为 posts 节点的结果,我们将有两个具有自动生成 ID 的子节点:
{
"posts": {
"-JRHTHaIs-jNPLXOQivY": {
"author": "gracehop",
"title": "Announcing COBOL, a New Programming Language"
},
"-JRHTHaKuITFIhnj02kE": {
"author": "alanisawesome",
"title": "The Turing Machine"
}
}
}