@Test public void testGetValueFlat() throws JsonParserException, ParsingException { JsonObject obj = JsonParser.object().from("{\"name\":\"John\",\"age\":30,\"cars\":{\"car1\":\"Ford\",\"car2\":\"BMW\",\"car3\":\"Fiat\"}}"); assertTrue("John".equals(JsonUtils.getValue(obj, "name"))); }
@Nonnull public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { List<String> keys = Arrays.asList(path.split("\\.")); object = getObject(object, keys.subList(0, keys.size() - 1)); if (null == object) throw new ParsingException("Unable to get " + path); Object result = object.get(keys.get(keys.size() - 1)); if (null == result) throw new ParsingException("Unable to get " + path); return result; }
JsonUtils { @Nonnull public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { List<String> keys = Arrays.asList(path.split("\\.")); object = getObject(object, keys.subList(0, keys.size() - 1)); if (null == object) throw new ParsingException("Unable to get " + path); Object result = object.get(keys.get(keys.size() - 1)); if (null == result) throw new ParsingException("Unable to get " + path); return result; } }
JsonUtils { @Nonnull public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { List<String> keys = Arrays.asList(path.split("\\.")); object = getObject(object, keys.subList(0, keys.size() - 1)); if (null == object) throw new ParsingException("Unable to get " + path); Object result = object.get(keys.get(keys.size() - 1)); if (null == result) throw new ParsingException("Unable to get " + path); return result; } private  JsonUtils(); }
JsonUtils { @Nonnull public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { List<String> keys = Arrays.asList(path.split("\\.")); object = getObject(object, keys.subList(0, keys.size() - 1)); if (null == object) throw new ParsingException("Unable to get " + path); Object result = object.get(keys.get(keys.size() - 1)); if (null == result) throw new ParsingException("Unable to get " + 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 Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException { List<String> keys = Arrays.asList(path.split("\\.")); object = getObject(object, keys.subList(0, keys.size() - 1)); if (null == object) throw new ParsingException("Unable to get " + path); Object result = object.get(keys.get(keys.size() - 1)); if (null == result) throw new ParsingException("Unable to get " + 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; }