2016-08-24 75 views
1

我已經設計了firebase中的拼合結構。Firebase拼合結構並創建pojo android

{ 
    "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系統創建POJO類此JSON。我該怎麼做?

2-對於當我在用戶json更新用戶名稱如「brinchen - > john」時,我如何將jhon與brinchen key中的成員json相匹配?

回答

0

您應該有POJO's處理解析。例如,你可以/應該具備以下條件:

// 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)我不完全喜歡火力地堡,,但在正常的JSON解析在Android,你會做以下幾點:

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

編輯:

基於火力地堡的文件,你的公共無效onDateChanged(DataSnapShot SNAP) ,你可以通過執行以下操作的結果轉換爲JSON(林不知道你的結構,但其像這樣 - 只有組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(); 
     } 
    } 
} 
+0

謝謝您的回答,但你應該閱讀本文件的https ://firebase.google.com/docs/database/android/structure-data – mustafasevgi

+0

是的。我知道這一點。我使用了基本的JSONObjects,而不是使用Firebase的修改過的JSON結構。 無論哪種方式,解析對象的機制似乎是根據我指定的onDataChanged()。 此外,通過在方法上獲取這些Firebase對象,您可以像解釋的那樣將它們移植到POJO中。 感謝您的領導! –