面臨的麻煩,
Android 開發中經常需要與Intenet通信獲取數據 ,中間交換格式,大家都喜歡Json, 如何將Json對像轉為Java的對像? 一個個屬性來解析可以實現,但對我來說這樣做太土了.
通過Java的反射可以很方便,高效,易讀的實現
先看一個Json對像
{ "content":[{
"level":1,
"status":"2",
"businessLicence":true,
"hygieneLicence":true,
"note":"note1",
"enterprise":"free",
"principal":"老韓",
"phone":"13366350377",
"time":1380862998588,
"address":"bj aa",
"latitude":38.112,
"longitude":116.002
}
,{
"level":2,
"status":"3",
"businessLicence":false,
"hygieneLicence":false,
"note":"note22222222222222222222222",
"enterprise":"鵬程萬裡",
"principal":"韓工",
"phone":"13366350377",
"time":1380962998588,
"address":"bj aa",
"latitude":38.112,
"longitude":116.002
}
,{
"level":3,
"status":"4",
"businessLicence":true,
"hygieneLicence":false,
"note":"海天3 note333333333333333333333333333",
"enterprise":"csdn",
"principal":"韓工",
"phone":"13366350377",
"time":1380943998588,
"address":"bj aa",
"latitude":38.112,
"longitude":116.002
}]
}
一個Json對像
對應的 Java對像
public class Content {
private int level;
private String status;
private boolean businessLicence;
private boolean hygieneLicence;
private String note;//
private String enterprise;
private String principal;
private long time;
private String address;
private double latitude;
private double longitude;
private String phone;
private final String[] properties = {"level",
"phone",
"status",
"businessLicence",
"hygieneLicence",
"note",
"enterprise",
"principal",
"time",
"address",
"latitude",
"longitude"
};
public Content() {
time = System.currentTimeMillis();
init();
}
public Content(JSONObject jObj) {
if (null == jObj) {
time = System.currentTimeMillis();
return;
}
init();
try {
fromJSONObject(jObj);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getCategory() {
// TODO Auto-generated method stub
return category;
}
public JSONObject toJSONObject() {
// TODO Auto-generated method stub
return null;
}
public Content fromJSONObject(JSONObject jObj) throws JSONException {
if (null == jObj)
return null;
Object obj;
for (String property : properties) {
if (jObj.has(property)) {
obj = jObj.get(property);
// 方法1:
// 比較文明,調用類的set 方法進行賦值.
setProperty("set" + property, obj);
// 方法2:
// 直接對屬性賦值,有點野蠻
// this.getClass().getDeclaredField(property).set(property, v);
// Field field = this.getClass().getDeclaredField(property);
// boolean accessible = field.isAccessible();
// field.setAccessible(true);
// field.set(this, v);
// field.setAccessible(accessible);
}
}
return this;
}
/** 下面是常用的老土的方法,全是體力活 :
*
*/
public Content fromJSONObject2(JSONObject jObj) throws JSONException {
if (null == jObj)
return null;
if (jObj.has("level")) {
level = jObj.getInt("level");
}
if (jObj.has("status")) {
status = jObj.getString("status");
}
if (jObj.has("businessLicence")) {
businessLicence = jObj.getBoolean("businessLicence");
}
if (jObj.has("hygieneLicence")) {
hygieneLicence = jObj.getBoolean("hygieneLicence");
}
if (jObj.has("note")) {
note = jObj.getString("note");
}
if (jObj.has("enterprise")) {
enterprise = jObj.getString("enterprise");
}
if (jObj.has("principal")) {
principal = jObj.getString("principal");
}
if (jObj.has("time")) {
time = jObj.getLong("time");
}
if (jObj.has("address")) {
address = jObj.getString("address");
}
if (jObj.has("latitude")) {
latitude = jObj.getDouble("latitude");
}
if (jObj.has("longitude")) {
longitude = jObj.getDouble("longitude");
}
return this;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public boolean isBusinessLicence() {
return businessLicence;
}
public void setBusinessLicence(boolean businessLicence) {
this.businessLicence = businessLicence;
}
public boolean isHygieneLicence() {
return hygieneLicence;
}
public void setHygieneLicence(boolean hygieneLicence) {
this.hygieneLicence = hygieneLicence;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getEnterprise() {
return enterprise;
}
public void setEnterprise(String enterprise) {
this.enterprise = enterprise;
}
public String getPrincipal() {
return principal;
}
public void setPrincipal(String principal) {
this.principal = principal;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
boolean isMe(String category) {
this.category.equals(category);
}
public String getContent() {
return note;
}
public Drawable getIcon() {
return null;
}
String getVillage() {
return null;
}
public String getCell() {
return null;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
// 反射代碼,
final private Map<String, Method> methodMap = new HashMap<String, Method>();// 以set開始的方法的map
// 可以直接獲取需要的set方法.
protected void init() {
Class<?> userClass = this.getClass();// Class.forName(this.getClass()); 加載類
Method[] methods = userClass.getDeclaredMethods();// 獲得類的方法集合
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().startsWith("set")) {
methodMap.put(methods[i].getName().toLowerCase(), methods[i]);
}
}
}
protected void setProperty(String property, Object v) {
Method method = methodMap.get(property.toLowerCase());
try {
method.invoke(this, v);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
對通上述的代碼,可以明顯發現,使用Java 反射帶來的好處