表 service_goods_base 字段如下:

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

方法如下:

  1.           if(StringUtil.isNotEmpty(servicePictureArray))
  2. {
  3. JSONArray array = JSONArray.fromObject(servicePictureArray);
  4. List<ServiceGoodsPicture> list = new ArrayList<ServiceGoodsPicture>();
  5. for (int i = 0; i < array.size(); i++) {
  6. Map<String, Object> object = (Map<String, Object>) array
  7. .get(i);
  8. ServiceGoodsPicture entity = ClassUtil.mapToEntity(ServiceGoodsPicture.class, object);
  9.  
  10. list.add(entity);
  11. }
  12. List<ServiceGoodsPicture> picList=serviceGoodsPictureService.saveServicePicBase(list, serviceId);
  13. sgbEntity.setPicList(picList);
  14. }

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

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

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

  1. public List<ServiceGoodsPicture> saveServicePicBase(List<ServiceGoodsPicture> list, String goodsId) {
  2. List<ServiceGoodsPicture> restList=new ArrayList<ServiceGoodsPicture>();
  3. for(int i = 0; i < list.size();i++)
  4. {
  5. ServiceGoodsPicture restPic=new ServiceGoodsPicture();
  6. ServiceGoodsPicture picture = list.get(i);
  7. picture.setSid(UUIDUtil.getUUID());
  8. picture.setPicCreateTime(new Date());
  9. picture.setServiceId(goodsId);
  10. picture.setSort(i);
  11. serviceGoodsPictureMapper.insertSelective(picture);
  12.  
  13. }
  14. return restList;
  15. }

  

map 与对象的转化函数:

  1. public static <T> T mapToEntity(Class<T> objClass,Map<String,Object> map){
  2. T entity = null;
  3. try {
  4. entity = objClass.newInstance();
  5. BeanInfo beanInfo = Introspector.getBeanInfo(entity.getClass());
  6. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  7.  
  8. for (PropertyDescriptor property : propertyDescriptors) {
  9. String key = property.getName();
  10. if (map.containsKey(key)) {
  11. Object value = map.get(key);
  12.  
  13. if(value instanceof JSONNull){
  14. value = null;
  15. }
  16.  
  17. Object convertValue = value;
  18. // 得到property对应的setter方法
  19. Method setter = property.getWriteMethod();
  20. Class<?> propertyType = property.getPropertyType();
  21. if ((propertyType == Double.class) || (propertyType == Double.TYPE)) {
  22. if (!(value instanceof Double))

                   if (!(value.toString()).matches("[0-9.]+"))
                      convertValue = 0;
                   else
                       convertValue = Double.valueOf(value.toString());

  1. } else if ((propertyType == Long.class) || (propertyType == Long.TYPE)) {
  2. if (!(value instanceof Long))
  3. convertValue = 0.0;
  4. } else if ((propertyType == Integer.class) || (propertyType == Integer.TYPE)) {
  5. if (!(value instanceof Integer) )
  6.  
  7. if (!(value.toString()).matches("[0-9]+"))
  8. convertValue = 0;
  9. else
  10. convertValue = Integer.valueOf((String) value);
  11.  
  12. } else if ((propertyType == Float.class) || (propertyType == Float.TYPE)) {
  13. if (!(value instanceof Float))
  14. convertValue = 0.0;
  15. } else if ((propertyType == Boolean.TYPE) || (propertyType == Boolean.class)) {
  16. if (!(value instanceof Boolean)) {
  17. if (("1".equals(value)) || ("Y".equalsIgnoreCase(value + "")) || ("Yes".equalsIgnoreCase(value + "")))
  18. convertValue = true;
  19. else
  20. convertValue = false;
  21. }
  22. } else if (propertyType == Date.class) {
  23. if ((!(value instanceof Date)) && (value != null)) {
  24. convertValue = null;
  25. }
  26. } else if (value instanceof net.sf.json.JSONNull){
  27.  
  28. }
  29. setter.invoke(entity, convertValue);
  30. }
  31. }
  32. } catch (InstantiationException e) {
  33. e.printStackTrace();
  34. } catch (IllegalAccessException e) {
  35. e.printStackTrace();
  36. } catch (IllegalArgumentException e) {
  37. e.printStackTrace();
  38. } catch (InvocationTargetException e) {
  39. e.printStackTrace();
  40. } catch (IntrospectionException e) {
  41. e.printStackTrace();
  42. }
  43. return entity;
  44. }
  45.  
  46. public static Map<String, Object> entityToMap(Object obj){
  47. if(obj == null)
  48. return null;
  49. Map<String, Object> map = new HashMap<String, Object>();
  50. try {
  51. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  52. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  53. for (PropertyDescriptor property : propertyDescriptors) {
  54. String key = property.getName();
  55. if (key.compareToIgnoreCase("class") == 0) {
  56. continue;
  57. }
  58. Method getter = property.getReadMethod();
  59. Object value = getter!=null ? getter.invoke(obj) : null;
  60. map.put(key, value);
  61. }
  62. } catch (Exception e) {
  63. }
  64. return map;
  65. }

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

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

  1.           else if ((propertyType == Integer.class) || (propertyType == Integer.TYPE)) {
  2. if (!(value instanceof Integer) )
  3.  
  4. if (!(value.toString()).matches("[0-9]+"))
  5. convertValue = 0;
  6. else
  7. convertValue = Integer.valueOf((String) value);
  8.  
  9. }

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. Flexbox兼容性语法汇总

    Flexbox版本 flexbox从第一次出现至今总共有三个语法版本,他们分别是: "display:box;"  —  2009年的老版本 "display:flexb ...

  2. 分享一个仅0.7KB的jQuery文本框输入提示插件

    由于项目需要,找过几个jQuery文本框输入提示插件来用,但总是有不满意的地方,要么体积较大,要么使用不便,要么会出现把提示文字作为文本框的值的情况.于是我们自己的开发团队制作了这个最精简易用的输入提 ...

  3. go get 下载的包放在哪里呢?

    有些问题,我以前都是似懂非懂,没有去弄个究竟!!!!! 这个习惯非常不好,搞得有些东西看似懂了,又不能百分之百说自己懂了,可能下次就弄不出来了,这样是不可取的. 不能有这种做事的风格. ------- ...

  4. 使用Eclipse载入一个现有的java项目

    下文的导入记录了在Eclipse环境中,要对已经下载好的java源码进行载入时的步骤.主要用于下载后的文件不方便进行拷贝到其它位置的情况.笔者几次用到这个顺序来载入项目,但是都是隔非常长时间才用一次, ...

  5. IDEA+MAVEN+testNG(reportNG)

    转载:http://www.cnblogs.com/aikachin/p/7765846.html 参考: http://blog.csdn.net/langsand/article/details/ ...

  6. Java8 更快的原子类:LongAdder(笔记)

    更快的原子类:LongAdder      大家对AtomicInteger的基本实现机制应该比较了解,它们是在一个死循环内,不断尝试修改目标值,知道修改成功,如果竞争不激烈,那么修改成功的概率就很高 ...

  7. Web service--百度百科

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...

  8. scrollBy 相对滚动

    scrollBy可以相对当前位置移动滚动条,而不是移动到绝对位置 scrollBy(0, 100); // 滚动条下移100px

  9. Oracle 存储过程调用返回游标的另一个存储过程。

    一个扩展存储过程调用另一个存储过程,示例: 被调用存储过程:最后会返回一个游标,游标返回一个值.调用这个存储过程的存储过程同样需要获取它. procedure SearchBill --根据到货单号查 ...

  10. linux归档压缩命令

    1.tar     tar    -cf    output.tar    file1.txt     file2.txt ..     tar    -rvf    output.tar    fl ...