该Jayway JsonPath图书馆阅读使用JSON路径值的支持。
例如:
String json = "...";Map<String, Object> book = JsonPath.read(json, "$.store.book[0]");System.out.println(book); // prints {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}Double price = JsonPath.read(json, "$.store.bicycle.price");System.out.println(price); // prints 19.95您还可以将JSON对象直接映射到类,例如在GSON或Jackson中:
Book book = JsonPath.parse(json).read("$.store.book[0]", Book.class);System.out.println(book); // prints Book{category='reference', author='Nigel Rees', title='Sayings of the Century', price=8.95}如果您想专门使用GSON或Jackson进行反序列化(默认为使用json-smart),也可以配置以下命令:
Configuration.setDefaults(new Configuration.Defaults() { private final JsonProvider jsonProvider = new JacksonJsonProvider(); private final MappingProvider mappingProvider = new JacksonMappingProvider(); @Override public JsonProvider jsonProvider() { return jsonProvider; } @Override public MappingProvider mappingProvider() { return mappingProvider; } @Override public Set<Option> options() { return EnumSet.noneOf(Option.class); }});有关更多详细信息,请参见文档。



