在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
误入歧途?如果没有,如何管理隐式财产设置/获取?
没有什么能阻止你创建一个用JSON设置属性的用户类,并用getter方法获取这些字段。那有什么问题? – 2012-02-13 15:41:17