title: JSON.getString doesn't return null
body: <p>I have a response coming back from a server and I am expecting a String value, so I wrote this for parsing it</p>
  
  <pre><code>public String getMessageFromServer(JSONObject response) {
      String msg = response.getString("message");
      return msg;
  }
  </code></pre>
  
  <p>then, when I use this in my code and get a <code>null</code> value from the server, the function doesn't return <code>null</code>, it returns <code>"null"</code> instead.</p>
  
  <p>I have seen <a href="https://github.com/douglascrockford/JSON-java/issues/5" rel="noreferrer">this bug report</a>, but I don't see a solution.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have a small hack to solve this but it's ugly and I'm looking for a better solution:</p>
  
  <pre><code>public String getMessageFromServer(JSONObject response) {
      Object msg = response.get("message");
      if(msg == null) {
           return null;
      }
      return (String) msg;
  }    
  </code></pre>
  
  <p><strong>EDIT #2:</strong></p>
  
  <p>after years, going back to this question, I see that I was not entirely wrong here and that <code>JSONObject</code> has a built in method for this.</p>
  
  <p>The way to get an optional value from a <code>JSONObject</code> is with using <a href="https://stleary.github.io/JSON-java/org/json/JSONObject.html#optString-java.lang.String-" rel="noreferrer">this method</a> <code>JSONObject.optString("message", DEF_VALUE);</code></p>