Simple JSON value parsing for Java 8

落花浮王杯 提交于 2019-12-06 22:04:29

A simple option would be to use JsonPath. Nested properties can be pulled using a path specifier like $.environments.DEV.maxSize:

long maxSize = JsonPath.parse(json).read("$.environments.DEV.maxSize", Long.class);

With Jackson, this can be done either with JsonNode:

long maxSize = new ObjectMapper()
    .readTree(json)
    .get("environments").get("DEV").get("maxSize").asLong();

Or if you have a class that represents the data, you can deserialize to that:

MyConfig myConfig = new ObjectMapper().readValue(json, MyConfig.class);
long maxSize = myConfig.getEnvironments().get(Environment.DEV).getMaxSize();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!