zj
2025-04-04 8ceb6cd5ba9d7f347f2070a3967f31cc070ef4ef
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
package kernel.util;
 
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
 
/**
 * 
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:json工具類
 * </p>
 
 */
public class JsonUtils {
    /**
     * 转成JSON字符串
     * 
     * @param obj
     * @return
     */
    public static String getJsonString(Object obj) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
 
    /**
     * 
     * @param <T>
     * @param jsonStr
     * @param T
     * @return
     */
    public static <T> T readJsonEntity(String jsonStr, Class<T> T) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
            return mapper.readValue(jsonStr, T);
 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * JsonNode
     * 
     * @param jsonStr
     * @see { 代码示例: String resultJson
     *      ="{'playerResults':[{'playerId':'111','gameId':'','tee':'0,0,0'},{'playerId':'ff80808137f7daac0137f7dd1ab80001','gameId':'','tee':'255,255,255'}]}";
     *      JsonNode jn=readJsonEntity(resultJson); jn=jn.get("playerResults");
     *      for (int i = 0; i < jn.size(); i++){ String
     *      playerId=jn.get(i).get("playerId").asText();
     *      logger.info("playerId="+playerId); } }
     * @return
     */
    public static JsonNode readJsonEntity(String jsonStr) {
        return readJsonEntity(jsonStr, JsonNode.class);
    }
    
    /**
     * 将POJO转换成JSON 
     * @param object
     * @return
     * @throws Exception 
     */ 
     public static String bean2Json(Object object) {
         String json = null;
         ObjectMapper mapper = new ObjectMapper();
         try {
             json = mapper.writeValueAsString(object);
         } catch (Exception e) {
             e.printStackTrace();
             return null;
         }
         return json;
     } 
    
     /**
      * 将JSON转换成POJO
     * @param <T>
      * @param json
      * @param beanClz POJO的Class
      * @return
     *  @throws Exception 
      */
     public static <T> T json2Object(String json, Class<T> beanClz) {
         ObjectMapper mapper = new ObjectMapper();
         mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); 
         mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true); 
         mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         T t = null;
         try {
             t = mapper.readValue(json, beanClz);
         } catch (Exception e) {
             try {t = beanClz.newInstance();} catch (Exception e1) {}
             e.printStackTrace();
             return null;
         }
         return t;
     } 
     
     /** 
         * json数组转List 
         * @param jsonStr 
         * @param valueTypeRef 
         * @return 
         */  
        public static <T> T readValue(String jsonStr, TypeReference<T> valueTypeRef) {  
            ObjectMapper objectMapper = new ObjectMapper();
            try {  
                return objectMapper.readValue(jsonStr, valueTypeRef);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
            return null;  
        }  
}