forked from baozhida/jmeter.bak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadJSON.java
71 lines (57 loc) · 1.67 KB
/
ReadJSON.java
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
import java.util.LinkedHashMap;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import net.minidev.json.JSONArray;
public class Test {
public static void main(String[] args) {
String jsonString = "{a:\"nnnnn\",b:{c:4441,d:dadwadw},e:[f:11,g:22],h:[],i:\"\"}";
String JsonPath,result,name,tmp;
int n,m,l,p;
//jsonString = prev.getResponseDataAsString();
//log.info("======:"+jsonString);
m=4;
JsonPath= "$.data["+m+"].gItems.length()";
result =Test.readjson(jsonString,JsonPath);
log.info("======================:"+result);
l = Integer.parseInt(result);
n = 0;
for (int i=0;i<le;i++){
JsonPath= "$.data["+n1+"].gItems["+i+"]";
n=i+1;
name = "mId_1_"+n;
vars.put(name,mId);
tmp = vars.get(name);
log.info("======================:"+tmp);
}
System.out.println(resultString);
}
public static String readjson(String json, String jsonPath) {
try
{
Object value = JsonPath.read(json, jsonPath, new Predicate[0]);
if (value instanceof Integer)
{
return value.toString();
}else if (value instanceof String)
{
return value.toString();
}else if (value instanceof Boolean)
{
return value.toString();
}else if (value instanceof JSONArray)
{
JSONArray arr = (JSONArray)value;
if(!arr.isEmpty()){return arr.toJSONString();}
return "";
}else if (value instanceof LinkedHashMap)
{
return value.toString();
}else{
return "";
}
}
catch(Exception e){
return "pathnotfound";
}
}
}