Java原生JSONObject实现JSON串与Java对象互转

JSON串转Java对象

通用Object

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
public static <T> T jsonToObject(String json, Class<T> c)  {
try {
JSONObject jsonObject = new JSONObject(json);
return (T) jsonToObject(jsonObject, c);
} catch (JSONException e) {
NLog.e(TAG, Log.getStackTraceString(e));
} catch (InstantiationException e) {
NLog.e(TAG, Log.getStackTraceString(e));
} catch (IllegalAccessException e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
return null;
}

//一个静态类,将json对象,转化为java
private static Object jsonToObject(JSONObject jsonObject, Class c) throws IllegalAccessException, InstantiationException {
Object obj = c.newInstance();
Iterator it = jsonObject.keys();
Field[] fields = c.getDeclaredFields();
while (it.hasNext()) {
String key = (String) it.next();
//判断字段
for (Field field : fields) {
if (field.getName().equals(key)) {
useSetter(c, field, jsonObject, obj);
break;
}
}
}
return obj;
}

//调用该类的setter方法
private static void useSetter(Class c, Field field, JSONObject jsonObject, Object obj) {
//获取字段类型以及字段名称
Class fieldType = field.getType();
String fieldName = field.getName();
String methodName = toMethodName(fieldName);
//System.out.println(methodName+"类型"+fieldType);
//调用方法
try {
Method method = c.getMethod(methodName, fieldType);
method.invoke(obj, jsonObject.get(fieldName));
} catch (NoSuchMethodException e) {
NLog.e(TAG, Log.getStackTraceString(e));
} catch (Exception e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
}

//将fieldName转化为方法名称
private static String toMethodName(String name) {
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
return methodName;
}

Map

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
public static Map<String, Object> jsonToMap(String json) {
if (TextUtils.isEmpty(json)) {
return null;
}
try {
Map<String, Object> params = new HashMap<>();
JSONObject jsonObject = new JSONObject(json);
return jsonObjectToMap(jsonObject, params);

} catch (JSONException e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
return null;
}

public static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) {
if (jsonObject == null) return null;
Map<String, Object> params = new HashMap<>();
return jsonObjectToMap(jsonObject, params);
}

private static Map<String, Object> jsonObjectToMap(JSONObject jsonObject, Map<String, Object> params) {
if (jsonObject == null || params == null) {
return params;
}
try {
for (Iterator<String> iterator = jsonObject.keys(); iterator.hasNext();) {
String key = iterator.next();
if (!TextUtils.isEmpty(key)) {
Object object = jsonObject.get(key);
if (object instanceof JSONObject) {
params.put(key, jsonObjectToMap((JSONObject) object, params));
} else {
params.put(key, jsonObject.get(key));
}
}
}
} catch (JSONException e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
return params;
}

Java对象转JSON串

通用Object

1

Map

1
2
3
4
5
6
7
8
9
10
11
12
13
public static String mapToJson(Map<String, String> map) {
if (map.isEmpty()) return "{}";
try {
JSONObject json = new JSONObject();
for (Map.Entry<String, String> entry : map.entrySet()) {
json.put(entry.getKey(), entry.getValue());
}
return json.toString();
} catch (JSONException e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
return "{}";
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :