我在我的Android应用程序中使用firebase。我在json中有以下用户数据结构:
{ "employee":
{
"empone" : {
"id" : "11",
"name" : "emponeone"
},
"emptwo" : {
"id" : 10,
"name" : "emptwo"
}
}
}
我想在我的arrylist中获取所有员工的姓名。但是当我从firebase读取数据时,我在Datasnapshot对象中得到了员工引用,我无法将其存储到我的jsonObject中。我试过以下:
String data1= String.valueOf(dataSnapshot.getValue());
但是这给了我以下格式的数据:
data1= {empone={name=emponeone, id=11}, emptwo={name=emptwo, id=10}}
无论如何,我只能在我的arraylist或任何其他格式中获取名称字段,以便按照我想要的方式获得员工结构?
答案 0 :(得分:2)
关注documentation。它有很好的例子和解释。
您需要一个类Employee
,其中包含一个名为name
的字符串,一个名为id
的字符串或字符串(因为您在示例中混合了这两个字符串,我不会&#39}不知道。)
最后,将其转换为:
class Employee {
String name;
String id;
public Employee(){
//needed for the getValue() method
}
//getters, setters, constructors
}
// Get Employee object and use the values to update the UI
Employee employee = dataSnapshot.getValue(Employee.class);
答案 1 :(得分:1)
您获得的输出是Java地图的输出,如下所述:AbstractMap#toString。
要将输出作为JSON,您可以使用
将Map转换为JSONJSONObject json = new JSONObject((Map)dataSnapshot.getValue());
答案 2 :(得分:0)
你dataSnapShot应该有孩子,你应该遍历它们:
for (DataSnapshot snapshotChild : snapshot.getChildren())
并使用employee.class
解析孩子,这将使用snapshotChild.getValue(employee.class)
将员工描述为Java Bean类,您可以使用bean类的get函数检索名称。
答案 3 :(得分:0)
实际上,我在计算机上模拟了您的问题。我在Firebase实时数据库中创建了确切的数据库,并试图读取它。
我发现针对两种不同情况有两种可能的解决方案。
在这种情况下,您只需要将datasnapshot转换为JSONObject,然后按其名称访问每个对象。
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference reference = firebaseDatabase.getReference();
reference = reference.child("employee");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
jsonObject = new JSONObject((Map) dataSnapshot.getValue());
try {
JSONObject empone = jsonObject.getJSONObject("empone");
String name = empone.getString("name");
JSONObject emptwo = jsonObject.getJSONObject("emptwo");
String name2 = empone.getString("name");
Log.i(TAG, "onDataChange: name: " + name);
} catch (JSONException e) {
Log.e(TAG, "onDataChange: JSON Exception: " + e.getLocalizedMessage());
e.printStackTrace();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e(TAG, "onCancelled: database error: " + databaseError.getMessage());
}
});
}
这是一种乏味且不推荐的方法,但确实可以解决您提到的问题
当您不知道项目的名称和数量时。 在这种情况下,您需要为JSONObjects创建一个模型类。 将每个JSONObject分配给该模型类并遍历数据快照 像这样:
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference reference = firebaseDatabase.getReference();
reference = reference.child("employee");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
//Getting User object from dataSnapshot
Employee employee = data.getValue(Employee.class);
Log.i(TAG, "onDataChange: empname: " + employee.getName());
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e(TAG, "onCancelled: database error: " + databaseError.getMessage());
}
});
这是推荐的方法,只需很少的信息即可完成工作!
您可以read completely about Firebase Database,从JSON简介到从Firestore删除文件全部集中在一处!