-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJsonPathHelloWorld.java
More file actions
69 lines (53 loc) · 1.64 KB
/
JsonPathHelloWorld.java
File metadata and controls
69 lines (53 loc) · 1.64 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
package fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
/**
* Json Path Hello World
*
*/
public class JsonPathHelloWorld {
public static void main(String[] args) {
// init class
Place place = new Place();
place.setName("World");
Human human = new Human();
human.setMessage("Hi");
human.setPlace(place);
// convert to json and from json
String jsonString = JSON.toJSONString(human);
Human newHuman = JSON.parseObject(jsonString, Human.class);
// use eval to get info
Object message = JSONPath.eval(newHuman, "$.message");
Object world = JSONPath.eval(newHuman, "$.place.name");
System.out.println(message + " " + world); // print Hi World
}
private static class Human {
private String message;
private Place place;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public void say() {
System.out.println();
System.out.println(getMessage() + " , " + getPlace().getName() + "!");
}
}
private static class Place {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}