表 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字符串保存到表中的方法的更多相关文章

  1. java解析json字符串详解(两种方法)

    一.使用JSONObject来解析JSON数据官方提供的,所以不需要导入第三方jar包:直接上代码,如下 private void parseJSONWithJSONObject(String Jso ...

  2. 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 ...

  3. Java实现微信菜单json字符串拼接

    Java实现微信菜单json字符串拼接 微信菜单拼接json字符串方法 >>>>>>>>>>>>>>>> ...

  4. <摘录>Gson对Java嵌套对象和JSON字符串之间的转换

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...

  5. Java对象转换成Json字符串是无法获得对应字段名

    问题: 代码中已经标注 @JSONField(name = "attrs") private String abc; public String getA() { return a ...

  6. 【Java_Spring】java解析多层嵌套json字符串

    java解析多层嵌套json字符串    

  7. JS解析json数据并将json字符串转化为数组的实现方法

    json数据在ajax实现异步交互时起到了很重要的作用,他可以返回请求的数据,然后利用客户端的js进行解析,这一点体现出js的强大,本文介绍JS解析json数据并将json字符串转化为数组的实现方法, ...

  8. 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中

    摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...

  9. mssql sqlserver sql对使用逗号分隔的字符串 转换为数据表的另类方法实现

    转自:http://www.maomao365.com/?p=10739 摘要: 下文讲述在sqlserver 对逗号分隔的字符串转换为数据表的另类方法实现,如下所示: 实验环境:sql server ...

随机推荐

  1. Windows数据备份软件Deltacopy-数据备份与还原

    官方网站:http://www.aboutmyip.com/AboutMyXApp/DeltaCopy.jsp System Requirements XP, 2000, 2003, 2008, Vi ...

  2. linux驱动摸索 --驱动框架初始化(结合韦东山视频教程)

    一.驱动框架 初始化:insmod 加载 1.确定主设备号: 分为静态和动态分配,其中LED_GPIO_SIZE 表示支持的次设备号数目,一般默认为1. 相关实现代码如下: int result; d ...

  3. VR开发者必看:4大最为值得关注的内容平台【转】

            时间 2016-01-19 14:12:57 原文  http://www.sfw.cn/xinwen/478369.html 主题 虚拟现实 Oculus 对很多有意涉及VR行业的内 ...

  4. scrapy-splash抓取动态数据例子十三

    一.介绍 本例子用scrapy-splash通过搜狗搜索引擎,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 ...

  5. 在线安装eclipse中html/jsp/xml editor插件 eclipseeditor

    1.打开eclipse中的help————>Install New Software 2.点击Add按钮,然后弹出一个框,第一个文本框可以随便写,第二个一定要写: http://download ...

  6. Docker默认存储路径修改

    Docker默认存储路径: # docker info...... Data loop file: /var/lib/docker/devicemapper/devicemapper/data.... ...

  7. eclipse中egit插件使用(转)

    1.eclipse和egit版本 eclipse使用的是kepler的SR1版,egit就是自带的那个版本,要详细的话,我就也只能把文件名贴出来了:eclipse-jee-kepler-SR1-win ...

  8. C++——动态分配内存问题

    class Obj { public: float score; ]; }; class Result { public: int id; ]; Obj obj[]; }; 合法,可动态分配内存给Re ...

  9. VS中 无法创建虚拟目录 本地IIS IIS Express 外部主机

    从前就有个疑问了,为什么我拉取别人写好的代码后就可以在IIS里面生成一个网站呢? 这里所谓的生成网站,是指包含了所有源代码文件的网站:相对地,发布网站,就是指包含被编译的源文件所得到的DLL文件的网站 ...

  10. linux tomacat 之部署 war包

    之前一篇写的是tomcat的linux安装,其中主要是jre 的问题,接下来讲讲 tomcat发布war包 这是一个相对简单的过程 打好war包后,将war包上传到/usr/local/tomcat/ ...