Helius
2021-01-06 8eb03554c07de7e268cf230249caf10922c6b47d
1
2
3
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.matrix.system.hive.plugin.util;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
 
import com.matrix.core.tools.StringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
/**
 * JSON对象操作工具类,格式转化,获取值(防止空异常)
 * 
 * @author Ron
 * @createTime 2014.08.30
 */
public class JSONUtils {
    
    public static String getString(JSONObject jsonparam, String key) {
        
        if (jsonparam == null) {
            return "";
        }
        try {
            return jsonparam.getString(key);
        }
        catch (Exception e) {
            return "";
        }
    }
    
    /****
     * 
     * @param jsonparam
     * @param key
     * @param str 为null返回默认值
     * @return
     */
    public static String getString(JSONObject jsonparam, String key, String str) {
        
        if (jsonparam == null) {
            return str;
        }
        try {
            String s = jsonparam.getString(key);
            if (StringUtils.isNotBlank(s)) {
                return StringUtils.trim(s);
            }
            return str;
        }
        catch (Exception e) {
            return str;
        }
    }
    
    public static int getInt(JSONObject jsonparam, String key) {
        
        if (jsonparam == null) {
            return 0;
        }
        try {
            return jsonparam.getInt(key);
        }
        catch (Exception e) {
            return 0;
        }
    }
    
    /**
     * 序列化对象
     * 
     * @param obj
     * @return
     */
    public static String setSer(Object obj) {
        
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream out;
        String serStr = "";
        try {
            out = new ObjectOutputStream(outputStream);
            out.writeObject(obj);
            serStr = outputStream.toString("ISO-8859-1");
            serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return serStr;
    }
    
    /**
     * 反序列化对象
     * 
     * @param serStr
     * @return
     */
    public static Object getSer(String serStr) {
        
        String redStr;
        InputStream inputStream = null;
        try {
            redStr = java.net.URLDecoder.decode(serStr, "UTF-8");
            inputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
        }
        catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(inputStream);
            return in.readObject();
        }
        catch (Exception e) {
            System.out.println("对象存在问题,请重新读取!");
        }
        return null;
    }
    
    public static long getLong(JSONObject jsonparam, String key) {
        
        if (jsonparam == null) {
            return 0;
        }
        try {
            return jsonparam.getLong(key);
        }
        catch (Exception e) {
            return 0;
        }
    }
    
    public static int getInt(JSONObject jsonparam, String key, int number) {
        
        if (jsonparam == null) {
            return number;
        }
        try {
            return jsonparam.getInt(key);
        }
        catch (Exception e) {
            return number;
        }
    }
    
    public static JSONArray getJSONArray(JSONObject jsonparam, String key) {
        JSONArray array = null;
        if (jsonparam == null) {
            array = new JSONArray();
        }
        else {
            try {
                array = jsonparam.getJSONArray(key);
            }
            catch (Exception e) {
                array = new JSONArray();
            }
        }
        return array;
    }
    
    public static JSONArray getJSONArray(String key) {
        
        if (StringUtils.isBlank(key)) {
            return new JSONArray();
        }
        try {
            return JSONArray.fromObject(key);
        }
        catch (Exception e) {
            return new JSONArray();
        }
    }
    
    public static JSONObject getJsonObject(String key) {
        
        if (StringUtils.isBlank(key))
            return new JSONObject();
        try {
            return JSONObject.fromObject(key);
        }
        catch (Exception e) {
            return new JSONObject();
        }
    }
    
    public static JSONObject getJsonObject(JSONObject jsonparam, String key) {
        
        if (StringUtils.isBlank(key))
            return new JSONObject();
        try {
            return jsonparam.getJSONObject(key);
        }
        catch (Exception e) {
            return new JSONObject();
        }
    }
    
    public static Double getDouble(JSONObject jsonparam, String key) {
        
        try {
            Double value = jsonparam.getDouble(key);
            return value;
        }
        catch (Exception e) {
        }
        return 0.0;
    }
    
  
    
    public static String getString(List<String> list, int i) {
        
        if (list == null) {
            return "";
        }
        
        try {
            return list.get(i);
        }
        catch (Exception e) {
            return "";
        }
        
    }
    
    
}