gpt4 book ai didi

android - 使用 firebase 的用户数据的 json 格式

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:51 25 4
gpt4 key购买 nike

我正在为我的 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}}

有什么方法可以让我只获取数组列表中的名称字段或任何其他格式来按照我想要的方式获取员工结构?

最佳答案

关注documentation .它有很好的例子和解释。

你需要一个 Employee 类,其中包含一个名为 name 的字符串,一个名为 id 的 int 或字符串(因为你将这两者混合在你的例子,我不知道。)最后,像这样转换它:

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);

关于android - 使用 firebase 的用户数据的 json 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37612038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com