设为首页 加入收藏

TOP

使用Java 反射,对类中成员变量赋值.将Json对像转为Java对像(一)
2014-11-24 01:40:25 来源: 作者: 【 】 浏览:0
Tags:使用 Java 反射 成员 变量 Json 转为 对像

面临的麻烦,


通过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("latitud

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 使用Eclipse编译 FBReade.. 下一篇实例 Android jni中调用Log输出调..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: