2012-02-13 138 views
1

在android中,我使用模型类和方法来处理数据操作。我的数据作为json从webservices中引入。我正在考虑使用JSONObjects来存储类级别属性的值的可能性。但是,我不知道如何使用JSONObj作为“持有者”变量并创建访问方法。我不想预先确定这些方法,如jsonRepository应持有的价值观,并不总是在设计时JSONObject作为类属性存储/检索

例如已知的,我想有: 公共类用户{ 私人的JSONObject jsonAttributes;

public User(String json) { 
    this.jsonAttributes= new JSONObject(json); 
} 

[IMPLICIT attribute access methods] 

public string Prop1() returns jsonAttributes.getString("prop1"); 
public string Prop1(String newProp1) returns jsonAttributes.putString("prop1",newProp1); 

public string Prop2() returns jsonRepository.getString("id"); 
public string Prop2(String newProp2) returns jsonAttributes.putString("prop2",newProp2); 
.... 

从该类话外,我只想访问属性...

User myUser = new User(someValidJson); 
String myString = myUser.Prop1 

误入歧途?如果没有,如何管理隐式财产设置/获取?

+1

没有什么能阻止你创建一个用JSON设置属性的用户类,并用getter方法获取这些字段。那有什么问题? – 2012-02-13 15:41:17

回答

0

正如在上面的评论中提到的那样,为什么不使用所有相关的memeber变量创建您的用户类,并简单地解析您的JSON数据以填充用户类中的离子形式。

有很多方法可以做到这一点,但我会考虑使用builder pattern,因为它很灵活,如果将来您的JSON数据发生变化,这可能会很有用。

+0

供参考。到建设者模式,thx。我选择了本地SQLite存储,而选择了一个检索JSON并填充数据库的服务。 – JBird 2012-02-20 18:40:55