How to do it...

  1. First, we define a JSON message to represent a User object:
{
"user": {
"email": "[email protected]",
"name": "Elder",
"profile": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
}
}
  1. 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}]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.45