JsonPathUtils.java
3.63 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
package com.lhcredit.common.utils;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.lhcredit.project.business.reportMake.ApiJsonPathService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class JsonPathUtils {
/**
* [0].xxx
* [0]
* [0].xxx.aaa
* @param path
* @return
*/
protected static Object path(Object object, String path){
Object eval = null;
try{
if (path.substring(0,1).contains("[")){
JSONArray arr= (JSONArray) object;
eval=JSONPath.eval(arr, "$" + path.replace("$",""));
}else{
eval = JSONPath.eval(object, "$." + path.replace("$.",""));
}
return null!=eval?eval.toString():null;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static String jsonPathString(JSONObject object, String path) {
try{
Object path1 = path(object, path);
return null!=path1?path1.toString():null;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static JSONObject getJSONObject(JSONObject object, String path) {
try{
Object path1 = path(object, path);
return JSONObject.parseObject(path1.toString());
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static JSONArray getJSONArray(JSONObject object, String path) {
try{
Object path1 = path(object, path);
return JSONArray.parseArray(path1.toString());
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static BigDecimal jsonPathBigDecimal(JSONObject object, String path) {
try{
Object path1 = path(object, path);
return null!=path1?new BigDecimal(path1.toString().replace("万","")) :null;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static Date jsonPathDate(JSONObject object, String path) {
try{
Object path1 = path(object, path);
return DateUtils.parseDate(path1.toString());
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static Integer jsonPathInteger(JSONObject object, String path) {
try{
Object path1 = path(object, path);
Integer integer = Integer.valueOf(path1.toString());
return integer;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static Double jsonPathDouble(JSONObject object, String path) {
try{
Object path1 = path(object, path);
return Double.valueOf(path1.toString());
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static <T> T jsonPathValue(JSONObject object,String path,Class<T> tClass){
try{
Object path1 = path(object, path);
return (T) path1;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static JSONObject paseJSONObj(Object object) {
if (object instanceof String) {
return JSON.parseObject(object.toString());
}else {
return JSONObject.parseObject(JSON.toJSONString(object));
}
}
}