上QQ阅读APP看书,第一时间看更新
How to do it...
- First, we define a JSON message to represent a User object:
{
"user": {
"email": "elder@eldermoraes.com",
"name": "Elder",
"profile": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
}
}
- Now, we create a method to read it and print the values we want:
public class JPointer {
public static void main(String[] args) throws IOException{
try (InputStream is =
JPointer.class.getClassLoader().getResourceAsStream("user.json");
JsonReader jr = Json.createReader(is)) {
JsonStructure js = jr.read();
JsonPointer jp = Json.createPointer("/user/profile");
JsonValue jv = jp.getValue(js);
System.out.println("profile: " + jv);
}
}
}
The execution of this code prints the following:
profile: [{"id":1},{"id":2},{"id":3}]