JavaBean2Json.java
3.91 KB
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
package com.viontech.utils;
import java.util.*;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
public class JavaBean2Json {
public static String javaBean2Json(Object obj) {
return JSONObject.fromObject(obj,getJsonConfig()).toString();
}
public static <T> T Json2JavaBean(String json,Class<T> clas) {
try {
ObjectMapper om = new ObjectMapper();
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return om.readValue(json, clas);
} catch (Exception e) {
JSONObject jsb = new JSONObject();
return (T) jsb.toBean(JSONObject.fromObject(json), clas, getJsonConfig());
}
}
/**
* json转List
* @param json
* @param clazz
* @param <T>
* @return
*/
public static <T> List<T> json2List(String json,Class<T> clazz) {
if(json.startsWith("[")){//json.indexOf("[")==0
JSONArray array = JSONArray.fromObject(json);
List<T> list = JSONArray.toList(array, clazz);
return list;
}
return null;
}
/**
* json转Array
* @param json
* @param clazz
* @param <T>
* @return
*/
public static <T> T[] json2Array(String json,Class<T> clazz) {
if(json.indexOf("[")==0){
JSONArray array = JSONArray.fromObject(json);
T[] ray = (T[]) JSONArray.toArray(array, clazz);
return ray;
}
return null;
}
private static JsonConfig getJsonConfig(){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source/* 属性的拥有者 */, String name /* 属性名字 */,Object value/* 属性值 */) {
if(value instanceof List){
List<Object> list = (List<Object>) value;
if (list == null || list.size()==0) {
return true;
}
}else if(value instanceof Map){
Map map = (Map) value;
if (map == null || map.size() == 0) {
return true;
}
}
return null == value || "".equals(value);
}
});
return jsonConfig;
}
public static void jsonToMap(String json, Map map) {
JSONObject jsonObject = JSONObject.fromObject(json);
populate(jsonObject, map);
}
private static void populate(JSONObject jsonObject, Map map) {
for (Iterator iterator = jsonObject.entrySet().iterator(); iterator
.hasNext();) {
String entryStr = String.valueOf(iterator.next());
String key = entryStr.substring(0, entryStr.indexOf("="));
String value = entryStr.substring(entryStr.indexOf("=") + 1,entryStr.length());
if (jsonObject.get(key).getClass().equals(JSONObject.class)) {
HashMap _map = new HashMap();
map.put(key, _map);
populate(jsonObject.getJSONObject(key), ((Map) (_map)));
} else if (jsonObject.get(key).getClass().equals(JSONArray.class)) {
ArrayList list = new ArrayList();
map.put(key, list);
populateArray(jsonObject.getJSONArray(key), list);
} else {
map.put(key, jsonObject.get(key));
}
}
}
private static void populateArray(JSONArray jsonArray, List list) {
for (int i = 0; i < jsonArray.size(); i++)
if (jsonArray.get(i).getClass().equals(JSONArray.class)) {
ArrayList _list = new ArrayList();
list.add(_list);
populateArray(jsonArray.getJSONArray(i), _list);
} else if (jsonArray.get(i).getClass().equals(JSONObject.class)) {
HashMap _map = new HashMap();
list.add(_map);
populate(jsonArray.getJSONObject(i), _map);
} else {
list.add(jsonArray.get(i));
}
}
/***
* 判断发过来的信息是否是json格式
*
* @param json
* @return
*/
public static boolean isJson(String json) {
try {
JSONObject.fromObject(json);
return true;
} catch (Exception e) {
return false;
}
}
}