解决JSONObject.toBean() 缺少参数报错问题

代码:

。。。。。。1

try {
if (Map.class.isAssignableFrom(beanClass)) {
if (JSONUtils.isNull(value)) {
setProperty(bean, key, value, jsonConfig);
} else if (value instanceof JSONArray) {
setProperty(bean, key, convertPropertyValueToCollection(key, value, jsonConfig, name, classMap, List.class), jsonConfig);
} else if (!String.class.isAssignableFrom(type) && !JSONUtils.isBoolean(type) && !JSONUtils.isNumber(type) && !JSONUtils.isString(type) && !JSONFunction.class.isAssignableFrom(type)) {
Class targetClass = findTargetClass(key, classMap);
targetClass = targetClass == null ? findTargetClass(name, classMap) : targetClass;
JsonConfig jsc = jsonConfig.copy();
jsc.setRootClass(targetClass);
jsc.setClassMap(classMap);
if (targetClass != null) {
setProperty(bean, key, toBean((JSONObject)value, jsc), jsonConfig);
} else {
setProperty(bean, key, toBean((JSONObject)value), jsonConfig);
}
} else if (jsonConfig.isHandleJettisonEmptyElement() && “”.equals(value)) {
setProperty(bean, key, (Object)null, jsonConfig);
} else {
setProperty(bean, key, value, jsonConfig);
。。。。。。。2
private static void setProperty(Object bean, String key, Object value, JsonConfig jsonConfig) throws Exception {
PropertySetStrategy propertySetStrategy = jsonConfig.getPropertySetStrategy() != null ? jsonConfig.getPropertySetStrategy() : PropertySetStrategy.DEFAULT;
propertySetStrategy.setProperty(bean, key, value);
}
}

。。。。。。。。3
public void setProperty(Object bean, String key, Object value) throws JSONException {
if (bean instanceof Map) {
((Map)bean).put(key, value);
} else {
try {
PropertyUtils.setSimpleProperty(bean, key, value);
//缺少字段抛异常
//缺少字段抛异常
//缺少字段抛异常
} catch (Exception var5) {
throw new JSONException(var5);
}
}

    }

解决:1
去掉不需要的字段
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
config.setExcludes(new String[]{//只要设置这个数组,指定过滤哪些字段。
// “l2Interfaces”,
// “OSInstalled”,
“ipInterfaces”,
“OSRunning”,
“fileSystems”,
“balanceMan”,
“endStation”
});
//如果不需要过滤,方法可用fromObject(o)
JSONObject jsonObject = JSONObject.fromObject(o,config);
tt = (TT)JSONObject.toBean(jsonObject, TT.class);
解决:2
重新封装包装类,将异常封装
public class PropertyStrategyWrapper extends PropertySetStrategy {
private PropertySetStrategy original;

public PropertyStrategyWrapper(PropertySetStrategy original) {
    this.original = original;
}

@Override
public void setProperty(Object o, String string, Object o1) throws JSONException {
    try {
        original.setProperty(o, string, o1);
    } catch (Exception ex) {
        //ignore
    }
}

}

JsonConfig cfg = new JsonConfig();
// 设置属性包装器
cfg.setPropertySetStrategy(new PropertyStrategyWrapper(PropertySetStrategy.DEFAULT));
// 设置要转换成的JavaBean
cfg.setRootClass(TT.class);
// 转换
tt = (TT)JSONObject.toBean(obj, cfg);

https://blog.csdn.net/weixin_33866037/article/details/92383943

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值