Skip to content

Commit deb701d

Browse files
committed
getString() now returns null instead of "null" when the value is null
1 parent b23ca24 commit deb701d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

JSONObject.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ public static String[] getNames(Object object) {
627627
* @throws JSONException if the key is not found.
628628
*/
629629
public String getString(String key) throws JSONException {
630+
if(isNull(key)){
631+
return null;
632+
}
630633
return get(key).toString();
631634
}
632635

Test.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public void testXML() throws Exception {
6767
assertEquals("<test><blank/><empty/></test>", XML.toString(j));
6868
}
6969

70+
public void testNull() throws Exception {
71+
JSONObject j;
72+
73+
j = new JSONObject("{\"message\":\"null\"}");
74+
assertFalse(j.isNull("message"));
75+
assertEquals("null", j.getString("message"));
76+
77+
j = new JSONObject("{\"message\":null}");
78+
assertTrue(j.isNull("message"));
79+
assertEquals(null, j.getString("message"));
80+
}
81+
7082
public void testJSON() throws Exception {
7183
Iterator it;
7284
JSONArray a;

0 commit comments

Comments
 (0)