JSONUtils { public static long getLong(JSONObject obj, String field) { return getLong(obj, field, 0L); }  static String getString(JSONObject obj, String field); static int getInt(JSONObject obj, String field); static long getLong(JSONObject obj, String field); static long getLong(JSONObject obj, String field, long defaultValue); static Boolean getBoolean(JSONObject obj, String field); static JSONObject getJSONObject(JSONObject obj, String field); static JSONArray getJSONArray(JSONObject obj, String field);  }
@Test public void testGetLong() throws ParseException { JSONParser parser = new JSONParser(); JSONObject jsonObject = (JSONObject) parser.parse("{\"salary\":2000,\"isYhd\":\"false\"}"); Assert.assertEquals(2000, JSONUtils.getLong(jsonObject, "salary")); }
@Test public void testGetLong1() throws ParseException { thrown.expect(ClassCastException.class); JSONParser parser = new JSONParser(); JSONObject jsonObject = (JSONObject) parser.parse("{\"salary\":\"2000\",\"isYhd\":\"false\"}"); Assert.assertEquals(2000, JSONUtils.getLong(jsonObject, "salary")); }