Java 中List 和Map 集合json 序列化
和Map 集合json 序列化
1.导⼊对应的和commons 的jar 包commons-beanutils-1.7.0.jar commons-collections-3.
2.jar commons-lang-2.
3.jar ezmorph-1.0.3.jar
json-lib-2.2.3-jdk15.jar
2.代码案例
⼀般来说,导⼊包后可以⽤JSONArray把List
当然,有时候模型类⾥会有Data类型数据,⽽通过上⾯⽅法序列化后得到的⽇期会被分隔成年、⽉、⽇、时、分、秒等⼦对象。不符合我们的期望。
这时,我们就需要写⼀个好⽤的⼯具类了。//从数据库中获取集合数据List <Model > modelList = modelService .getRecordByModelower (ModelOwer );JSONArray json = JSONArray .fromObject (mod
elList );
1
2
3
测试代码
当然,我做项⽬时遇到过⼀个问题,就是当我⽤mybatis返回⼀个map时,时间转json格式⽤⽹上通⽤的map转json⽅法不管⽤,所以⾃⼰写了个⽐较笨的⽅法来解决这⼀问题。import net .sf .json .processors .JsonValueProcessor ;import java .text .SimpleDateFormat ;import java .util .*;/** * Created by ZP on 2019/4/6. */public class JsonDateValueProcessor implements JsonValueProcessor { private String format = "yyyy-MM-dd HH:mm:ss"; public JsonDateValueProcessor () { super (); } public JsonDateValueProcessor (String format ) { // ⾃⼰需要的格式 super (); this .format = format ; } private Object process (Object value ) { if (value instanceof Date ) { SimpleDateFormat sdf = new SimpleDateFormat (format , Locale .CHINA ); return sdf .format (value ); } return value == null ? "" : value .toStrin
g (); } @Override public Object processArrayValue (Object o , JsonConfig jsonConfig ) { return process (o ); } @Override public Object processObjectValue (String s , Object o , JsonConfig jsonConfig ) { return process (o ); }}
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @Test public void testgetTaskByProvice (){ String province = "⼴西省"; List <Model > modelList
= modelService .getTaskByProvice (province ); JsonConfig jsonConfig = new JsonConfig (); jsonConfig .registerJsonValueProcessor (Date .class , new JsonDateValueProcessor ()); JSONArray jsonArray = JSONArray .fromObject (modelList ,jsonConfig ); System .out .println (jsonArray ); }
1
2
3
4
5
6
7
8
9
测试Map转json的⽤法
参考博客/** * Created by ZP on 2019/4/11. */public class JsonUtil { /*因为从mybatis ⾥取出封装成Map 的⽇期不是Data ,故写此⽅法来封装json */ public static List listMapToJson (List <Map > mapList ) { List jsonList = new ArrayList (); for (Map map :mapList ) { StringBuilder stringBuilder = new StringBuilder (); stringBuilder .append ("{"); Iterator <String > iter = map .keySet ().iterator (); while (iter .hasNext ()) { String key = iter .next (); String value = map .get (key ).toString (); stringBuilder .append ("\""+key +"\""+":"+"\""+value +"\""); if (iter .hasNext ()){ stringBuilder .append (","); } } stringBuilder .append ("}"); jsonList .add (stringBuilder ); } return jsonList ; }}
4
5
6
7
8
9
10
js获取json的key和value11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 @Test public void testMapToJson (){ String ModelOwer = "lin"; List <Map > mapList = taskPublishService .getListMapModel (ModelOwer ); System .out .println (mapList .toString ()); List jsonList = JsonDateValueProcessor .listMapToJson (mapList ); System .out .println (jsonList .toString ()); }
1
2
3
4
5
6
7
8
9
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论