我在firebase中设计了这个扁平结构。
{
"groups": {
"alpha": {
"members": {
"brinchen": true,
"mchen": true
},
"name": "Alpha Tango"
},
"bravo": {
"members": {
"brinchen": true
},
"name": "Bravo Romeo"
},
"charlie": {
"members": {
"hmadi": true,
"mchen": true
},
"name": "Charlie Whiskey"
},
"delta": {
"name": "Delta Kilo"
},
"echo": {
"name": "Echo Lima"
},
"foxtrot": {
"name": "Foxtrot November"
}
},
"users": {
"brinchen": {
"groups": {
"alpha": true,
"bravo": true
},
"name": "Byambyn Rinchen"
},
"hmadi": {
"groups": {
"charlie": true
},
"name": "Hamadi Madi"
},
"mchen": {
"groups": {
"alpha": true,
"charlie": true
},
"name": "Mary Chen"
}
}
}
我有两个问题:
1-我想在Android中为这个json创建pojo类。我该怎么办?
2-对于ex,当我在用户json中更新用户名如“brinchen - > john”时,如何将jhon与brinchen键中成员中的json组匹配?
答案 0 :(得分:0)
你应该有 POJO来处理解析。例如,您可以/应该具有以下内容:
// Group or Team, you name it
public class Group {
private String id;
private String name;
private List<String> members;
public Group(){}
public Group(JSONObject object) throws JSONException {
this.id = object.getString("id");
this.name = object.getString("name");
members = new ArrayList<>();
JSONArray list = object.getJSONArray("members");
if(list!=null){
for (int i = 0; i < list.length(); i++) {
members.add(((JSONObject)list.get(i)).getString("name"));
}
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getMembers() {
return members;
}
public void setMembers(List<String> members) {
this.members = members;
}
}
public class User {
private String id;
private String name;
private List<String> groups;
public User(){}
public User(JSONObject object) throws JSONException {
this.id = object.getString("id");
this.name = object.getString("name");
groups = new ArrayList<>();
JSONArray list = object.getJSONArray("groups");
if(list!=null){
for (int i = 0; i < list.length(); i++) {
groups.add(((JSONObject)list.get(i)).getString("groupId"));
}
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getGroups() {
return groups;
}
public void setGroups(List<String> groups) {
this.groups = groups;
}
}
3)我不是完全喜欢Firebase,但是在Android上正常的JSON解析,你会做以下事情:
JSONObject teamInformation = Service.getTeamInformation();
ArrayList<Group> myGroup = new ArrayList<>();
JSONArray groupList = teamInformation.getJSONObject("groups");
if(groupList!=null){
for (int i = 0; i < groupList.length(); i++) {
myGroup.add( new Group((JSONObject)groupList.get(i)) );
}
}
ArrayList<User> myUsers = new ArrayList<>();
JSONArray userList = teamInformation.getJSONObject("users");
if(userList!=null){
for (int i = 0; i < userList.length(); i++) {
myUsers.add( new User((JSONObject)userList.get(i)) );
}
}
编辑:
基于Firebase文档,在 public void onDateChanged(DataSnapShot snap)上 您可以通过执行以下操作将结果转换为JSON(我不确定您的结构,但它的类似这样 - 仅限于Groups JSON部分):
public void onDateChanged(DataSnapShot snap) {
for (DataSnapshot alert : alerts.getChildren()) {
System.out.println(alert.child("id").getValue();
System.out.println(alert.child("name").getValue();
for (DataSnapshot recipient : alert.child("members").getChildren()) {
System.out.println(recipient.child("name").getValue();
}
}
}