json 转换实体对象
import javax.servlet.http.HttpServletRequest;
import com.aliyuncs.utils.StringUtils;
import com.sinosoft.cms.entity.CoreOrganization;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JavaIdentifierTransformer;
public void coreImport(JSONObject requestJson, HttpServletRequest request) {
String data = requestJson.getString("data");
JSONObject jsonObject =JSONObject.fromObject(data);
//CoreOrganization entInfo = (CoreOrganization)JSONObject.toBean(jsonObject, CoreOrganization.class);//会导致参数不对对象为空
CoreOrganization entInfo = (CoreOrganization)JSONObject.toBean(jsonObject, getJsonConfig(CoreOrganization.class));
}
private JsonConfig getJsonConfig(Class clazz) {
JsonConfig config = new JsonConfig();
config.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {
@Override
public String transformToJavaIdentifier(String str) {
if(StringUtils.isEmpty(str)){
return str;
}
return str.substring(0,1).toLowerCase().concat(str.substring(1));
}
});
config.setRootClass(clazz);
return config;
}