ZeroLiYi 2 anos atrás
pai
commit
95ca847647

+ 129 - 0
src/main/java/com/unissoft/utils/JsonToObjectUtils.java

@@ -0,0 +1,129 @@
1
+package com.unissoft.utils;
2
+
3
+import com.fasterxml.jackson.databind.JavaType;
4
+import com.fasterxml.jackson.databind.ObjectMapper;
5
+import com.unissoft.webservice.model.TPoints;
6
+import org.slf4j.Logger;
7
+import org.slf4j.LoggerFactory;
8
+
9
+import java.text.SimpleDateFormat;
10
+import java.util.ArrayList;
11
+import java.util.List;
12
+
13
+public class JsonToObjectUtils {
14
+
15
+    final static Logger logger  =  LoggerFactory.getLogger(JsonToObjectUtils.class );
16
+
17
+    /**
18
+     * 将字符串转换成list
19
+     * @param str 要转换的字符json串
20
+     * @param packageName 实体的全包名称
21
+     * @return list对象
22
+     */
23
+    public static ArrayList<Object> jsonToObjects(String str, String packageName){
24
+        ArrayList<Object> object = null;
25
+        try {
26
+            ObjectMapper mapper = new ObjectMapper();
27
+            mapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
28
+            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Class.forName(packageName));
29
+            object = mapper.readValue(str, javaType);
30
+        } catch (Exception e) {
31
+            e.printStackTrace();
32
+            logger.info("json转换异常  "+e.getMessage());
33
+        }
34
+        return object;
35
+    }
36
+    /**
37
+     * 将字符串转换成list
38
+     * @param str 要转换的字符json串
39
+     * @param classs 实体
40
+     * @return list对象
41
+     */
42
+    public static ArrayList<Object> jsonToObjects(String str, Class<?> classs){
43
+        ArrayList<Object> object = null;
44
+        try {
45
+            ObjectMapper mapper = new ObjectMapper();
46
+            mapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
47
+            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, classs);
48
+            object = mapper.readValue(str, javaType);
49
+        } catch (Exception e) {
50
+            e.printStackTrace();
51
+            logger.info("json转换异常  "+e.getMessage());
52
+        }
53
+        return object;
54
+    }
55
+
56
+    /**
57
+     * 将字符串转换成单个对象
58
+     * @param str 要转换的字符json串
59
+     * @param packageName 实体的全包名称
60
+     * @return 返回单个对象
61
+     */
62
+    public static Object jsonToObject(String str, String packageName){
63
+        Object object = null;
64
+        try {
65
+            ObjectMapper mapper = new ObjectMapper();
66
+            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
67
+            object = mapper.readValue(str, Class.forName(packageName));
68
+        } catch (Exception e) {
69
+            e.printStackTrace();
70
+            logger.info("json转换异常  "+e.getMessage());
71
+        }
72
+        return object;
73
+    }
74
+    /**
75
+     * 将字符串转换成单个对象
76
+     * @param str 要转换的字符json串
77
+     * @param classs 实体
78
+     * @return 返回单个对象
79
+     */
80
+    public static Object jsonToObject(String str, Class<?> classs){
81
+        Object object = null;
82
+        try {
83
+            ObjectMapper mapper = new ObjectMapper();
84
+            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
85
+            object = mapper.readValue(str, classs);
86
+        } catch (Exception e) {
87
+            e.printStackTrace();
88
+            logger.info("json转换异常  "+e.getMessage());
89
+        }
90
+        return object;
91
+    }
92
+
93
+    /**
94
+     *
95
+     * @param str json字符串
96
+     * @param classs 要转换的类
97
+     * @param diverse 转换的时间类型
98
+     * @return
99
+     */
100
+    public static ArrayList<Object> jsonToObjectsDiverse(String str, Class<?> classs,String diverse){
101
+        ArrayList<Object> object = null;
102
+        try {
103
+            ObjectMapper mapper = new ObjectMapper();
104
+            mapper.setDateFormat(new SimpleDateFormat(diverse));
105
+            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, classs);
106
+            object = mapper.readValue(str, javaType);
107
+        } catch (Exception e) {
108
+            e.printStackTrace();
109
+            logger.info("json转换异常  "+e.getMessage());
110
+        }
111
+        return object;
112
+    }
113
+
114
+    /**
115
+     * list转换字符串
116
+     * @param points list参数
117
+     * @return
118
+     */
119
+    public static String listToString(List<TPoints> points){
120
+        try {
121
+            ObjectMapper mapper = new ObjectMapper();
122
+            return mapper.writeValueAsString(points);
123
+        } catch (Exception e) {
124
+            e.printStackTrace();
125
+            logger.info("json转换异常  "+e.getMessage());
126
+        }
127
+        return null;
128
+    }
129
+}

+ 79 - 0
src/main/java/com/unissoft/webservice/model/TPoints.java

@@ -0,0 +1,79 @@
1
+package com.unissoft.webservice.model;
2
+
3
+import java.math.BigDecimal;
4
+import java.util.Date;
5
+
6
+public class TPoints {
7
+    private BigDecimal temp;
8
+
9
+    private String x;
10
+
11
+    private String y;
12
+
13
+    private String z;
14
+
15
+    /**
16
+     * 温度值 单位:摄氏度;坏点值以250表示;精确到小数点后1位;
17
+     * @return temp 温度值 单位:摄氏度;坏点值以250表示;精确到小数点后1位;
18
+     */
19
+    public BigDecimal getTemp() {
20
+        return temp;
21
+    }
22
+
23
+    /**
24
+     * 温度值 单位:摄氏度;坏点值以250表示;精确到小数点后1位;
25
+     * @param temp 温度值 单位:摄氏度;坏点值以250表示;精确到小数点后1位;
26
+     */
27
+    public void setTemp(BigDecimal temp) {
28
+        this.temp = temp;
29
+    }
30
+
31
+    /**
32
+     * 测温点在仓内的X坐标代表长度 代表列
33
+     * @return X 测温点在仓内的X坐标代表长度 代表列
34
+     */
35
+    public String getX() {
36
+        return x;
37
+    }
38
+
39
+    /**
40
+     * 测温点在仓内的X坐标代表长度 代表列
41
+     * @param x 测温点在仓内的X坐标代表长度 代表列
42
+     */
43
+    public void setX(String x) {
44
+        this.x = x == null ? null : x.trim();
45
+    }
46
+
47
+    /**
48
+     * 测温点在仓内的Y坐标宽度方向 代表行
49
+     * @return Y 测温点在仓内的Y坐标宽度方向 代表行
50
+     */
51
+    public String getY() {
52
+        return y;
53
+    }
54
+
55
+    /**
56
+     * 测温点在仓内的Y坐标宽度方向 代表行
57
+     * @param y 测温点在仓内的Y坐标宽度方向 代表行
58
+     */
59
+    public void setY(String y) {
60
+        this.y = y == null ? null : y.trim();
61
+    }
62
+
63
+    /**
64
+     * 测温点在仓内的Z坐标高度
65
+     * @return Z 测温点在仓内的Z坐标高度
66
+     */
67
+    public String getZ() {
68
+        return z;
69
+    }
70
+
71
+    /**
72
+     * 测温点在仓内的Z坐标高度
73
+     * @param z 测温点在仓内的Z坐标高度
74
+     */
75
+    public void setZ(String z) {
76
+        this.z = z == null ? null : z.trim();
77
+    }
78
+
79
+}