zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package com.shlanbao.tzsc.utils.tools;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
 
import org.apache.log4j.Logger;
 
/**
 * JSON本身已经有很多方法,例如
 *<br>  jsonStr ="{intCount:30,strDesc:"DESC",objUser:{...},arrUsers:[{},{}]}"
 *<br>    JSONObject jsonObject =JSONObject.fromObject(jsonStr);
 *<br>    jsonObject.getInt("intCount");
 *<br>    jsonObject.getString("strDesc");
 *<br>    jsonObject.getJSONObject("objUser");
 *<br>    jsonObject.getJSONArray("arrUsers");
 *
 * @author adam.sun 
 */
public class JSONUtil {
    
      private static final Logger log = Logger.getLogger(JSONUtil.class);
    /**
     * 把JSONArray形式的数组转为javabean对象数组
     * @param reqString 输入的字符串形式[{},{},{}]
     * @param clazz javabeean的类名
     * @return Object 需要转变为自己的类型
     */
    @SuppressWarnings("unchecked")
    public static Object JSONString2ObjectArray(String reqString,Class clazz) {
        
        //String 到 JSONObject
        JSONArray ja =JSONArray.fromObject(reqString);
        //JSONObject 到 Bean
        return JSONArray.toArray(ja, clazz);
        
    }
 
    /**
     * 把JSONObject形式的数组转为javabean对象
     * @param reqString 输入的字符串形式{}
     * @param clazz javabeean的类名
     * @return Object 需要转变为自己的类型
     */
    @SuppressWarnings("unchecked")
    public static Object JSONString2Bean(String reqString,Class clazz) {
        
        //String 到 JSONObject
        JSONObject ja =JSONObject.fromObject(reqString);
        //JSONObject 到 Bean
        return JSONObject.toBean(ja, clazz);
         
    }
    
    
     /**
      *从requeets里取得变量参数
     * @param reader requet.getReader();
     * @throws IOException 
     */
    public static JSONObject getRequestJSONObject(BufferedReader reader) throws IOException {
        StringBuffer jb = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            jb.append(line);
        }
        return  JSONObject.fromObject(jb.toString());
    }
    
    
    /**
     * 把对象中的部分属性剔出掉,比如关联的一个对象 eg:
     *  <br>Object yourObject=.......;
     *  <br>String[] kickProperties = {"userName","userCode"};     //不需要转换的字段   
        <br>JsonConfig config = JSONUtil.kickProperty(kickProperties, TSysUser.class);
        <br>String string = JSONObject.fromObject(yourObject,config).toString();
        <br>OR
        <br>List string = JSONArray.fromObject(yourObject,config).toString();
        
     * @param kickProperties 需要剔出的属性列表
     * @param Class clazz javaBean 的类名
     * @return JsonConfig
     */
    @SuppressWarnings("unchecked")
    public static JsonConfig kickProperty(final String[] kickProperties,Class clazz) {
        
        if(kickProperties== null){
            return null;
        }
        
        JsonConfig jsonConfig = new JsonConfig();        
        jsonConfig.setRootClass( clazz );
        jsonConfig.setJsonPropertyFilter(new PropertyFilter(){
        public boolean apply( Object source, String name, Object value )
        {
            
            for (int i = 0; i < kickProperties.length; i++) {
                if( kickProperties[i].equalsIgnoreCase( name )  ){
                    //log.debug(" Your kill property is"+name);
                    return true;
                  }                  
            }
              
              return false;
           }
        });
        return jsonConfig;
    }
    
    /**
     * 把对象中的部分属性剔出掉,比如关联的一个对象(仅仅去掉指定类的属性) eg:
     *  <br>Object yourObject=.......;
     *  <br>String[] kickProperties = {"userName","userCode"};     //不需要转换的字段   
        <br>JsonConfig config = JSONUtil.kickProperty(kickProperties, TSysUser.class.getName());
        <br>String string = JSONObject.fromObject(yourObject,config).toString();
        <br>OR
        <br>List string = JSONArray.fromObject(yourObject,config).toString();
        
     * @param kickProperties 需要剔出的属性列表
     * @param Class clazz javaBean 的类名
     * @return JsonConfig
     */
    @SuppressWarnings("unchecked")
    public static JsonConfig kickProperty(final String[] kickProperties,final String className) {
        
        if(kickProperties== null){
            return null;
        }
        
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new PropertyFilter(){
        public boolean apply( Object source, String name, Object value )
        {
            if(!source.getClass().getName().equals(className)){
                return false; //不是本类的全部放行
            }
            for (int i = 0; i < kickProperties.length; i++) {
                
                if( kickProperties[i].equalsIgnoreCase( name )  ){
                    //log.debug(" Your kill property is"+name);
                    return true;
                  }                  
            }
              
              return false;
           }
        });
        return jsonConfig;
    }
 
 
    
    
    /**
     * 仅仅保留对象中的给定字段,比如关联的一个对象(仅仅去掉指定类的属性) eg:
     *  <br>Object yourObject=.......;
     *  <br>String[] kickProperties = {"userName","userCode"};     //不需要转换的字段   
        <br>JsonConfig config = JSONUtil.holdProperty(kickProperties, TSysUser.class.getName());
        <br>String string = JSONObject.fromObject(yourObject,config).toString();
        <br>OR
        <br>List string = JSONArray.fromObject(yourObject,config).toString();
        
     * @param holdProperties 需要剔出的属性列表
     * @param className  javaBean 的类名
     * @return JsonConfig
     */
    @SuppressWarnings("unchecked")
    public static JsonConfig holdProperty(final String[] holdProperties,final String className) {
        
        if(holdProperties== null){
            return null;
        }
        
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new PropertyFilter(){
        public boolean apply( Object source, String name, Object value )
        {            
                    
            //log.debug(source.getClass().getName()+" ==== "+className+"==="+name);
            if(!source.getClass().getName().equals(className)){
                return false; //不是本类的全部放行
            }
                
            for (int i = 0; i < holdProperties.length; i++) {
                
                //log.debug( holdProperties[i]+" equals "+name+"");
                if( holdProperties[i].equalsIgnoreCase( name )  ){
                    //log.debug(" Your hold property is "+name);
                    return false;
                  }                  
            }
            return true;
           }
        });
        return jsonConfig;
    }
    
    /**
     * 仅仅保留对象中的给定字段,比如关联的一个对象(仅仅去掉指定类的属性) eg:
     *  <br>Object yourObject=.......;
     *  <br>String[] kickProperties = {"userName","userCode"};     //不需要转换的字段   
        <br>JsonConfig config = JSONUtil.holdProperty(kickProperties, TSysUser.class.getName());
        <br>String string = JSONObject.fromObject(yourObject,config).toString();
        <br>OR
        <br>List string = JSONArray.fromObject(yourObject,config).toString();
        
     * @param holdProperties 需要剔出的属性列表
     * @param className  javaBean 的类名
     * @return JsonConfig
     */
    @SuppressWarnings("unchecked")
    public static JsonConfig holdProperty(final String[] holdProperties) {
        
        if(holdProperties== null){
            return null;
        }
        
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new PropertyFilter(){
        public boolean apply( Object source, String name, Object value )
        {            
            //log.debug("===================================="+name);
                for (int i = 0; i < holdProperties.length; i++) {
                    
                    //log.debug( holdProperties[i]+" equals "+name+"");
                    if( holdProperties[i].equalsIgnoreCase( name )  ){
                        //log.debug(" Your hold property is "+name);
                        return false;
                      }                  
                }
            
            
            return true;
           }
        });
        return jsonConfig;
    }
 
 
    /**
     * json string 转换为 map 对象
     * @param jsonObj
     * @return
     */
    public static Map<Object, Object> jsonToMap(Object jsonObj) {
        JSONObject jsonObject = JSONObject.fromObject(jsonObj);
        Map<Object, Object> map = (Map)jsonObject;
        return map;
    }
    
 
}