JsonNode是Jackson库中的一个类,用于解析和操作json数据,是一个树形结构的数据,可以用于读写json文件、数据的验证、数据的过滤、数据的查询等。

1、基本使用

在使用JsonNode时需要先将JSON字符串解析成JsonNode对象,可以使用ObjectMapper 进行解析:


String json = "{ \"name\" : \"John\", \"age\" : 30 }";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);

其中JsonNode对象的属性可以用get()方法获取,数字类型可以用asInt()方法获取类型对应的值:


JsonNode nameNode = node.get("name");
String name = nameNode.asText();
JsonNode ageNode = node.get("age");
int age = ageNode.asInt();

还可以表示数组和对象


String json = "{ \"name\" : \"John\", \"age\" : 30, \"hobbies\" : [ \"Coding\", \"Swimming\" ] }";
JsonNode node = mapper.readTree(json);
JsonNode hobbiesNode = node.get("hobbies");
for (JsonNode hobbyNode : hobbiesNode) {
    String hobby = hobbyNode.asText();
    System.out.println(hobby);
}

2、数据的操作

添加、删除属性
使用JsonNode的with()方法返回一个新的JsonNode对象,新对象中包含原来的数据和新增的属性:


String json = "{ \"name\" : \"John\", \"age\" : 30 }";
JsonNode node = mapper.readTree(json);
JsonNode newNode = node.with("gender", "male");

也可以使用set()方法将原来的属性值修改:


JsonNode ageNode = node.get("age");
if (ageNode != null && ageNode.isInt()) {
    ((ObjectNode)node).set("age", ageNode.asInt() + 1);
}

删除属性使用remove()方法:


((ObjectNode)node).remove("age");

过滤数据
JsonNode提供了filter()、findParents()、findParentOf()等方法过滤数据


String json = "{ \"name\" : \"John\", \"age\" : 30, \"hobbies\" : [ \"Coding\", \"Swimming\" ] }";
JsonNode node = mapper.readTree(json);
JsonNode hobbiesNode = node.path("hobbies").filter(n -> n.asText().equals("Coding")).get(0);

findParents()方法可以返回指定属性的父级节点:


JsonNode parentsNode = node.findParents("hobbies");

数据的查询
JsonNode提供了at()、findValue()、path()进行数据的查询,at()方法可以根据JsonPath表达式查询数据:


String json = "{ \"name\" : { \"first\" : \"John\", \"last\" : \"Doe\" }, \"age\" : 30 }";
JsonNode node = mapper.readTree(json);
JsonNode lastNameNode = node.at("/name/last");

findValue()方法可以查询指定属性名的值:


JsonNode nameNode = node.findValue("name");

异常处理

在使用JsonNode时,有可能出现解析错误、类型转换错误等异常情况,可以通过try-catch块来进行异常处理:


try {
    JsonNode node = mapper.readTree(json);
} catch (JsonProcessingException e) {
    System.out.println("解析错误:" + e.getMessage());
} catch (IOException e) {
    System.out.println("IO错误:" + e.getMessage());
} catch (Exception e) {
    System.out.println("其他错误:" + e.getMessage());
}

GitHub 加速计划 / js / json
37
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6 binary -> binary_t Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 20 天前
f3dc4684 Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 26 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐