SchemaUtils { public static JSONObject findFirstKeyJSONObject(JSONObject jo, String key) { if (jo.has(key)) { try { JSONObject jsonObject = jo.getJSONObject(key); return jsonObject; } catch (JSONException ex) { throw new RuntimeException(ex); } } @SuppressWarnings("unchecked") Iterator<String> keyIterator = jo.keys(); while (keyIterator.hasNext()) { String childKey = keyIterator.next(); JSONArray childJa = null; try { childJa = jo.getJSONArray(childKey); } catch (JSONException ex) { } if (childJa != null) { JSONObject result = findFirstKeyJSONObject(childJa, key); if (result != null) { return result; } continue; } JSONObject childJo = null; try { childJo = jo.getJSONObject(childKey); } catch (JSONException ex) { } if (childJo != null) { JSONObject result = findFirstKeyJSONObject(childJo, key); if (result != null) { return result; } } } return null; } private  SchemaUtils(); static String jarResourceFileToString(String resource); static String inputStreamToString(InputStream inputStream); static boolean checkValidKeys(JSONObject jo, Fields fields); static void checkValidKeysEx(JSONObject jo, Fields fields); static boolean checkValidKeys(JSONObject jo, Collection<Fields> fieldsCollection); static boolean checkValidKeys(JSONObject jo, List<Fields> fieldsCollection); static boolean checkValidKeysEx(JSONObject jo, Collection<Fields> fieldsCollection); static boolean checkValidKeysExHelper(JSONObject jo, Collection<Fields> fieldsCollection); static boolean checkValidKeysEx(JSONObject jo, List<Fields> fieldsCollection); static Set<String> getSetOfJSONKeys(JSONObject jo); static Map<String, String> convertFieldToType(Map<String, Type> fieldToType); static JSONArray findFirstKeyJSONArray(JSONObject jo, String key); static JSONArray findFirstKeyJSONArray(JSONArray ja, String key); static JSONObject findFirstKeyJSONObject(JSONObject jo, String key); static JSONObject findFirstKeyJSONObject(JSONArray ja, String key); static Map<String, String> extractMap(JSONObject jo); static JSONObject createJSONObject(Map<String, String> map); static List<String> getStringsFromJSONArray(JSONArray jsonStringArray); static List<String> getTags(JSONObject jo); static final String FIELD_TAGS; }
@Test public void findFirstKeyJSONObjectSimple() throws Exception { final String joString = "{\"a\":{\"b\":\"c\"}}"; JSONObject jo = new JSONObject(joString); JSONObject joFind = SchemaUtils.findFirstKeyJSONObject(jo, "a"); Assert.assertEquals(1, joFind.length()); Assert.assertEquals("c", joFind.getString("b")); }
@Test public void findFirstKeyJSONObjectTestNestJSONObject() throws Exception { final String joString = "{\"a\":{\"b\":{\"c\":\"d\"}}}"; JSONObject jo = new JSONObject(joString); JSONObject joFind = SchemaUtils.findFirstKeyJSONObject(jo, "b"); Assert.assertEquals(1, joFind.length()); Assert.assertEquals("d", joFind.getString("c")); }
@Test public void findFirstKeyJSONObjectTestNestedJSONArray() throws Exception { final String joString = "{\"a\":[{\"b\":{\"c\":\"d\"}}]}"; JSONObject jo = new JSONObject(joString); JSONObject joFind = SchemaUtils.findFirstKeyJSONObject(jo, "b"); Assert.assertEquals(1, joFind.length()); Assert.assertEquals("d", joFind.get("c")); }