java前端传入的json字符串保存到表中的方法
表 service_goods_base 字段如下:

传入的json 字符串: servicePictureArray : [{"picServiceUrl": "http://qimg.app.yiguanjiaclub.org/20180308/a48210dc7bfe4b34b3d7de114ef01f85","mainPage": "1"}]
方法如下:
if(StringUtil.isNotEmpty(servicePictureArray))
{
JSONArray array = JSONArray.fromObject(servicePictureArray);
List<ServiceGoodsPicture> list = new ArrayList<ServiceGoodsPicture>();
for (int i = 0; i < array.size(); i++) {
Map<String, Object> object = (Map<String, Object>) array
.get(i);
ServiceGoodsPicture entity = ClassUtil.mapToEntity(ServiceGoodsPicture.class, object); list.add(entity);
}
List<ServiceGoodsPicture> 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<ServiceGoodsPicture> saveServicePicBase(List<ServiceGoodsPicture> list, String goodsId) {
List<ServiceGoodsPicture> restList=new ArrayList<ServiceGoodsPicture>();
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> T mapToEntity(Class<T> objClass,Map<String,Object> map){
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 instanceof JSONNull){
value = null;
}
Object convertValue = value;
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
Class<?> propertyType = property.getPropertyType();
if ((propertyType == Double.class) || (propertyType == Double.TYPE)) {
if (!(value instanceof Double))
if (!(value.toString()).matches("[0-9.]+"))
convertValue = 0;
else
convertValue = Double.valueOf(value.toString());
} else if ((propertyType == Long.class) || (propertyType == Long.TYPE)) {
if (!(value instanceof Long))
convertValue = 0.0;
} else if ((propertyType == Integer.class) || (propertyType == Integer.TYPE)) {
if (!(value instanceof Integer) )
if (!(value.toString()).matches("[0-9]+"))
convertValue = 0;
else
convertValue = Integer.valueOf((String) value);
} else if ((propertyType == Float.class) || (propertyType == Float.TYPE)) {
if (!(value instanceof Float))
convertValue = 0.0;
} else if ((propertyType == Boolean.TYPE) || (propertyType == Boolean.class)) {
if (!(value instanceof Boolean)) {
if (("1".equals(value)) || ("Y".equalsIgnoreCase(value + "")) || ("Yes".equalsIgnoreCase(value + "")))
convertValue = true;
else
convertValue = false;
}
} else if (propertyType == Date.class) {
if ((!(value instanceof Date)) && (value != null)) {
convertValue = null;
}
} else if (value instanceof net.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();
}
return entity;
}
public static Map<String, Object> entityToMap(Object obj){
if(obj == null)
return null;
Map<String, Object> map = new HashMap<String, Object>();
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) {
}
return map;
}
如果 bean 的 propertyType 是 Integer ,map 中的value 传的不是 Integer : 则做一下判断:
如果value 是只包括 0-9 的字符,则把 value 转化成 Integer ,赋值给 convertValue , 否则 convertValue 赋值是 0.
else if ((propertyType == Integer.class) || (propertyType == Integer.TYPE)) {
if (!(value instanceof Integer) )
if (!(value.toString()).matches("[0-9]+"))
convertValue = 0;
else
convertValue = Integer.valueOf((String) value);
}
java前端传入的json字符串保存到表中的方法的更多相关文章
- java解析json字符串详解(两种方法)
一.使用JSONObject来解析JSON数据官方提供的,所以不需要导入第三方jar包:直接上代码,如下 private void parseJSONWithJSONObject(String Jso ...
- java普通对象和json字符串的互转
一.java普通对象和json字符串的互转 java对象---->json 首先创建一个java对象: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- Java实现微信菜单json字符串拼接
Java实现微信菜单json字符串拼接 微信菜单拼接json字符串方法 >>>>>>>>>>>>>>>> ...
- <摘录>Gson对Java嵌套对象和JSON字符串之间的转换
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...
- Java对象转换成Json字符串是无法获得对应字段名
问题: 代码中已经标注 @JSONField(name = "attrs") private String abc; public String getA() { return a ...
- 【Java_Spring】java解析多层嵌套json字符串
java解析多层嵌套json字符串
- JS解析json数据并将json字符串转化为数组的实现方法
json数据在ajax实现异步交互时起到了很重要的作用,他可以返回请求的数据,然后利用客户端的js进行解析,这一点体现出js的强大,本文介绍JS解析json数据并将json字符串转化为数组的实现方法, ...
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
- mssql sqlserver sql对使用逗号分隔的字符串 转换为数据表的另类方法实现
转自:http://www.maomao365.com/?p=10739 摘要: 下文讲述在sqlserver 对逗号分隔的字符串转换为数据表的另类方法实现,如下所示: 实验环境:sql server ...
随机推荐
- 转:Android推送技术研究
Android推送技术研究 字数5208 阅读4026 评论5 喜欢35 前言 最近研究Android推送的实现, 研究了两天一夜, 有了一点收获, 写下来既为了分享, 也为了吐槽. 需要说明的是有些 ...
- Win7如何查看自己得Win7版本号
如何查看Windows 7详细系统版本号? --Windows 7系统知识100问之七十一 责任编辑:姜惠田作者:IT168 老姜 2009-08-05 前言:微软新一代操作系统Windows 7 ...
- wifi破解到局域网渗透
本文转自 _博客 一,密码破解 wifi破解最主要的还是抓握手包破解(不要给我说某某钥匙的“分享”). wifi认证主要分为四步: 1,无线客户端与ap连接时,首先发送一个认证请求包 2,ap收到请求 ...
- 别样JAVA学习(六)继承下(2.3)异常下
1.RuntimeException Exception中有一个特殊的子类异常RuntimeException执行时异常. 假设在函数内容抛出该异常,函数上能够不用声明.编译一样通过. 假设在函数上声 ...
- 【DB2】NOT IN使用中的大坑
1.环境准备 ------建表TB DROP TABLE TB; CREATE TABLE TB ( ID INTEGER, LEVEL_DETAIL ) ); INSERT INTO TB (ID, ...
- C++/C混合编程
C与C++混合编程 C++ 是在 C 语言的基础上发展起来的.在某种程度上,我们可将 C++ 看做 C 的一种扩展.在本质上,二者的数据类型和函数调用惯例都是一致的,因此 C 与 C++ 混合编译也是 ...
- 用BSF + Beanshell使Java程序能够运行字符串形式的代码(转载)
BSF(Bean Scripting Framework)最初是IBM Alpha工作组的项目,后来贡献给了Apache,BSF使Java能够更好的与各种流行脚本语言整合,实现不同语言之间的交互功能. ...
- 阿里云创建RAM子账号
操作步骤 创建子账号 主账号导航至 访问控制 > 用户管理 页面.如下图所示: 单击右上角的 新建用户,如下图所示: 填写弹出框的各配置项,如下图所示: 单击 确定,即可创建子账号. 允许子账号 ...
- Quartz简介 用 Quartz 进行作业调度
http://www.ibm.com/developerworks/cn/java/j-quartz/现代的 Web 应用程序框架在范围和复杂性方面都有所发展,应用程序的每个底层组件也必须相应地发展. ...
- Html Table用JS导出excel格式问题 导出EXCEL后单元格里的000412341234会变成412341234 7-14 会变成 2018-7-14(7月14) 自定义格式 web利用table表格生成excel格式问题 js导出excel增加表头、mso-number-format定义数据格式 数字输出格式转换 mso-number-format:"\@"
Html Table用JS导出excel格式问题 我在网上找的JS把HTML Tabel导出成EXCEL.但是如果Table里的数字内容为0开的的导成Excel后会自动删除0,我想以text的格式写入 ...