JavaBean2Json.java
6.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
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
package com.viontech.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;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;
@SuppressWarnings({"unchecked","deprecation"})
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;
}
}
public static Object mulMapVal(Object object,String keys) {
if (object != null) {
String[] key = keys.split(",");
for (int i=0;i<key.length;i++) {
if (object instanceof Map) {
Map dbobj = (Map) object;
object = dbobj.get(key[i]);
}
}
return object;
}
return null;
}
public static Map<String, Object> transBean2Map(Object obj) {
if(obj == null){
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
System.out.println("transBean2Map Error " + e);
}
return map;
}
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
String json="{\"id\":\"32\", \"vehicle\" : {\n" +
" \"global_feature\" : {\n" +
" \"AlignParameters\" : null, \n" +
" \"FeatureVector\" : null\n" +
" }, \n" +
" \"image\" : \"全景1\", \n" +
" \"local_feature\" : {\n" +
" \"FeatureVector\" : null, \n" +
" \"nFeatureCount\" : 1097913940\n" +
" }, \n" +
" \"plate\" : {\n" +
" \"char_count\" : 7, \n" +
" \"color_code\" : \"2\", \n" +
" \"color_name\" : \"蓝色\", \n" +
" \"head_text\" : \"京\", \n" +
" \"image\" : \"全景1\", \n" +
" \"other_text\" : \"RS5T48\", \n" +
" \"rect\" : {\n" +
" \"bottom\" : 0.8336397, \n" +
" \"left\" : 0.7838542, \n" +
" \"right\" : 0.83177084, \n" +
" \"top\" : 0.8134191\n" +
" }, \n" +
" \"score\" : 0, \n" +
" \"text\" : \"京RS5T48\", \n" +
" \"type_code\" : \"02\"\n" +
" }, \n" +
" \"rect\" : {\n" +
" \"bottom\" : 0.89966905, \n" +
" \"left\" : 0.66406256, \n" +
" \"right\" : 0.95156246, \n" +
" \"top\" : 0.61216915\n" +
" }\n" +
" }\n" +
"\t\t}\n";
JavaBean2Json.jsonToMap(json,map);
System.out.println("map = " + map);
String plate_text =(String) JavaBean2Json.mulMapVal(map, "vehicle,plate,text");
System.out.println("plate_text = " + plate_text);
}
}