java json 表_java前端传入的json字符串保存到表中的方法

表 service_goods_base 字段如下:

ec20eedb13e9cec34ee895e791b9553b.png

传入的json 字符串: servicePictureArray  :  [{"picServiceUrl": "http://qimg.app.yiguanjiaclub.org/20180308/a48210dc7bfe4b34b3d7de114ef01f85","mainPage": "1"}]

方法如下:

if(StringUtil.isNotEmpty(servicePictureArray))

{

JSONArray array=JSONArray.fromObject(servicePictureArray);

List list = new ArrayList();for (int i = 0; i < array.size(); i++) {

Map object = (Map) array

.get(i);

ServiceGoodsPicture entity= ClassUtil.mapToEntity(ServiceGoodsPicture.class, object);

list.add(entity);

}

List picList=serviceGoodsPictureService.saveServicePicBase(list, serviceId);

sgbEntity.setPicList(picList);

}

先用  JSONArray.fromObject(servicePictureArray) 把json 字符串转化成 一个json数组array。 对应一个 bean 的 list 。

获取array数组中的每个元素,保存在 object 中。 再通过 mapToEntity 方法 把 map 的对象 object 转化成 表的对象 entity 。 并且把这个对象保存在list 中。

调用另一个service 的方法,获取list 的每个元素,保存在 bean 的 picture 对象中,并给picture 对象其他的属性赋值。最后 调用mybatis 的方法, 插入picture 对象。保存在表中。

public List saveServicePicBase(List list, String goodsId) {

List restList=new ArrayList();

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

{

ServiceGoodsPicture restPic=new ServiceGoodsPicture();

ServiceGoodsPicture picture = list.get(i);

picture.setSid(UUIDUtil.getUUID());

picture.setPicCreateTime(new Date());

picture.setServiceId(goodsId);

picture.setSort(i);

serviceGoodsPictureMapper.insertSelective(picture);

}

return restList;

}

map 与对象的转化函数:

public static T mapToEntity(Class objClass,Mapmap){

T entity= null;try{

entity=objClass.newInstance();

BeanInfo beanInfo=Introspector.getBeanInfo(entity.getClass());

PropertyDescriptor[] propertyDescriptors=beanInfo.getPropertyDescriptors();for(PropertyDescriptor property : propertyDescriptors) {

String key=property.getName();if(map.containsKey(key)) {

Object value=map.get(key);if(value instanceofJSONNull){

value= null;

}

Object convertValue=value;//得到property对应的setter方法

Method setter =property.getWriteMethod();

Class> propertyType =property.getPropertyType();if ((propertyType == Double.class) || (propertyType ==Double.TYPE)) {if (!(value instanceofDouble))

if (!(value.toString()).matches("[0-9.]+"))

convertValue = 0;

else

convertValue = Double.valueOf(value.toString());

}else if ((propertyType == Long.class) || (propertyType ==Long.TYPE)) {if (!(value instanceofLong))

convertValue= 0.0;

}else if ((propertyType == Integer.class) || (propertyType ==Integer.TYPE)) {if (!(value instanceofInteger) )if (!(value.toString()).matches("[0-9]+"))

convertValue= 0;elseconvertValue=Integer.valueOf((String) value);

}else if ((propertyType == Float.class) || (propertyType ==Float.TYPE)) {if (!(value instanceofFloat))

convertValue= 0.0;

}else if ((propertyType == Boolean.TYPE) || (propertyType == Boolean.class)) {if (!(value instanceofBoolean)) {if (("1".equals(value)) || ("Y".equalsIgnoreCase(value + "")) || ("Yes".equalsIgnoreCase(value + "")))

convertValue= true;elseconvertValue= false;

}

}else if (propertyType == Date.class) {if ((!(value instanceof Date)) && (value != null)) {

convertValue= null;

}

}else if (value instanceofnet.sf.json.JSONNull){

}

setter.invoke(entity, convertValue);

}

}

}catch(InstantiationException e) {

e.printStackTrace();

}catch(IllegalAccessException e) {

e.printStackTrace();

}catch(IllegalArgumentException e) {

e.printStackTrace();

}catch(InvocationTargetException e) {

e.printStackTrace();

}catch(IntrospectionException e) {

e.printStackTrace();

}returnentity;

}public static MapentityToMap(Object obj){if(obj == null)return null;

Map map = new HashMap();try{

BeanInfo beanInfo=Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] propertyDescriptors=beanInfo.getPropertyDescriptors();for(PropertyDescriptor property : propertyDescriptors) {

String key=property.getName();if (key.compareToIgnoreCase("class") == 0) {continue;

}

Method getter=property.getReadMethod();

Object value= getter!=null ? getter.invoke(obj) : null;

map.put(key, value);

}

}catch(Exception e) {

}returnmap;

}

如果 bean 的 propertyType 是 Integer ,map 中的value 传的不是 Integer : 则做一下判断:

如果value 是只包括 0-9 的字符,则把 value 转化成 Integer ,赋值给 convertValue , 否则 convertValue 赋值是 0.

else if ((propertyType == Integer.class) || (propertyType ==Integer.TYPE)) {

if (!(value instanceofInteger) )

if (!(value.toString()).matches("[0-9]+"))

convertValue = 0;

elseconvertValue =Integer.valueOf((String) value);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值