@Test public void testGetValues() throws JsonParserException, ParsingException { JsonObject obj = JsonParser.object().from("{\"id\":\"0001\",\"type\":\"donut\",\"name\":\"Cake\",\"ppu\":0.55,\"batters\":{\"batter\":[{\"id\":\"1001\",\"type\":\"Regular\"},{\"id\":\"1002\",\"type\":\"Chocolate\"},{\"id\":\"1003\",\"type\":\"Blueberry\"},{\"id\":\"1004\",\"type\":\"Devil's Food\"}]},\"topping\":[{\"id\":\"5001\",\"type\":\"None\"},{\"id\":\"5002\",\"type\":\"Glazed\"},{\"id\":\"5005\",\"type\":\"Sugar\"},{\"id\":\"5007\",\"type\":\"Powdered Sugar\"},{\"id\":\"5006\",\"type\":\"Chocolate with Sprinkles\"},{\"id\":\"5003\",\"type\":\"Chocolate\"},{\"id\":\"5004\",\"type\":\"Maple\"}]}"); JsonArray arr = (JsonArray) JsonUtils.getValue(obj, "topping"); List<Object> types = JsonUtils.getValues(arr, "type"); assertTrue(types.contains("Chocolate with Sprinkles")); }
@Nonnull public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException { List<Object> result = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.getObject(i); result.add(getValue(obj, path)); } return result; }
JsonUtils { @Nonnull public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException { List<Object> result = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.getObject(i); result.add(getValue(obj, path)); } return result; } }
JsonUtils { @Nonnull public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException { List<Object> result = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.getObject(i); result.add(getValue(obj, path)); } return result; } private  JsonUtils(); }
JsonUtils { @Nonnull public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException { List<Object> result = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.getObject(i); result.add(getValue(obj, path)); } return result; } private  JsonUtils(); @Nonnull static Object getValue(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static String getString(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static Number getNumber(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path); }
JsonUtils { @Nonnull public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException { List<Object> result = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.getObject(i); result.add(getValue(obj, path)); } return result; } private  JsonUtils(); @Nonnull static Object getValue(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static String getString(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static Number getNumber(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path); @Nonnull static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path); static final JsonObject EMPTY_OBJECT; static final JsonArray EMPTY_ARRAY; static final String EMPTY_STRING; }