官术网_书友最值得收藏!

How to do it...

You need to perform the following steps to try this recipe:

  1. First, we define a JSON message to represent the User object:
{
"user": {
"email": "elder@eldermoraes.com",
"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);
}
}
}
  1. The execution of this code prints the following:
profile: [{"id":1},{"id":2},{"id":3}]

主站蜘蛛池模板: 阿拉尔市| 万荣县| 灌阳县| 鄂州市| 翁牛特旗| 林芝县| 灵璧县| 宁海县| 永丰县| 宁安市| 鄢陵县| 余姚市| 江陵县| 光泽县| 扶绥县| 绥化市| 永靖县| 绥宁县| 泸水县| 扎赉特旗| 礼泉县| 陇西县| 玉环县| 赫章县| 万载县| 香河县| 青冈县| 嘉兴市| 观塘区| 林西县| 沙坪坝区| 连山| 凤冈县| 靖州| 绥阳县| 晋江市| 昌宁县| 哈尔滨市| 泗阳县| 屯昌县| 永嘉县|