用反射設置屬性方法。自己一個例子

private List<AeNoticeListVO> getListVo(Object info,String storeId,String storeName) throws Exception

{

List<AeNoticeListVO> rL = new ArrayList<AeNoticeListVO>();

JSONArray  aInfo = (JSONArray)info;

if(aInfo.size()==0)

{

return rL;

}

for (int i = 0; i < aInfo.size(); i++)

{

JSONObject jItem = aInfo.getJSONObject(i);

AeNoticeListVO aevo = new AeNoticeListVO();

Field[] field = aevo.getClass().getDeclaredFields();

for(int j=0;j<field.length;j++)

{

String  value = getJasonValue(jItem,field[j].getName());

if(null!=value)

{

Method m = aevo.getClass().getMethod("set"+captureName(field[j].getName()),String.class);

                    m.invoke(aevo, value);

}

}

aevo.setStore_id(storeId);

aevo.setStore_name(storeName);

aevo.setUpdate_time(new Date());

rL.add(aevo);

}

return rL;

}


注意自動的set的方法第一個字母都是大寫

protected String captureName(String name) {

        char[] cs=name.toCharArray();

        cs[0]-=32;

        return String.valueOf(cs);

        

    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章