Extractor { public static Double[] extractLocation(JSONObject obj) { Double longitude = 0.0; Double latitude = 0.0; try { if (!obj.isNull("coordinates")) { if (!obj.getJSONObject("coordinates").isNull("coordinates")) { JSONArray coords = obj.getJSONObject("coordinates").getJSONArray("coordinates"); longitude = coords.getDouble(0); latitude = coords.getDouble(1); } } else { JSONArray places = obj.getJSONObject("place").getJSONObject("bounding_box").getJSONArray("coordinates").getJSONArray(0); if (places.length() > 2) { Double topLeftLong = places.getJSONArray(0).getDouble(0); Double topLeftLat = places.getJSONArray(0).getDouble(1); Double lowerLeftLong = places.getJSONArray(1).getDouble(0); Double lowerLeftLat = places.getJSONArray(1).getDouble(1); Double topRightLong = places.getJSONArray(2).getDouble(0); Double topRightLat = places.getJSONArray(2).getDouble(1); Double lowerRightLong = places.getJSONArray(3).getDouble(0); Double lowerRightLat = places.getJSONArray(3).getDouble(1); longitude = (topLeftLong + lowerLeftLong + topRightLong + lowerRightLong) / 4; latitude = (topLeftLat + lowerLeftLat + topRightLat + lowerRightLat) / 4; } } Double[] longLat = {longitude, latitude}; return longLat; } catch (JSONException e) { System.err.println("No Correct JSON File"); e.printStackTrace(); return null; } }  static Double[] extractLocation(JSONObject obj);  }
@Test public void testextractLocations() throws JSONException { Double[] extract1= Extractor.extractLocation(new JSONObject(json1)); Double[] extract2= Extractor.extractLocation(new JSONObject(json2)); System.out.println(extract1[0]+" " + extract1[1]); Assert.assertTrue(38.4614716==extract1[1]); Assert.assertTrue(27.2147884==extract1[0]); Double testLong = (16.182180+16.182180+16.577511+16.577511)/4; Double testLat = ( 48.117666+48.322574+48.322574+48.117666)/4; System.out.println("ext2[0]: "+ extract2[0] + " testLong: "+ testLong); System.out.println("ext2[1]: "+ extract2[1] + " testLat: "+ testLat); Assert.assertTrue(testLong.equals(extract2[0])); Assert.assertTrue(testLat.equals(extract2[1])); }