Android Json and Null Values

How to determine when json is null? for example: [{"username": null}, {"username": "null"}]

The first case is an unused username, and the second is named "null". But if you try to restore them, both values ​​will result in the string "null"

JSONObject json = new JSONObject("{\"hello\":null}"); json.put("bye", JSONObject.NULL); Log.e("LOG", json.toString()); Log.e("LOG", "hello="+json.getString("hello") + " is null? " + (json.getString("hello") == null)); Log.e("LOG", "bye="+json.getString("bye") + " is null? " + (json.getString("bye") == null)); 

Magazine exit

 {"hello":"null","bye":null} hello=null is null? false bye=null is null? false 
+65
java json android
May 14 '12 at 18:12
source share
6 answers
+182
May 14 '12 at 18:15
source share

Since JSONObject # getString returns a value if the given key exists, by definition it is not null. It is for this reason that JSONObject.NULL exists: to represent a null JSON value.

 json.getString("hello").equals(JSONObject.NULL); // should be false json.getString("bye").equals(JSONObject.NULL); // should be true 
+13
May 14 '12 at 18:16
source share

For android, it raises a JSONException if such a mapping does not exist. Therefore, you cannot directly call this method.

 json.getString("bye") 

if the data may be empty (the key may not exist) try

 json.optString("bye","callback string"); 

or

 json.optString("bye"); 

instead.

In your demo code

 JSONObject json = new JSONObject("{\"hello\":null}"); json.getString("hello"); 

this is what you get, this String "null" is not null.

use your sound

 if(json.isNull("hello")) { helloStr = null; } else { helloStr = json.getString("hello"); } 
+11
Mar 11 '13 at 8:36
source share

first check with isNull() .... if it doesn't work then try belows

and also you have JSONObject.NULL to check for a null value ...

  if ((resultObject.has("username") && null != resultObject.getString("username") && resultObject.getString("username").trim().length() != 0) { //not null } 

and in your case also check resultObject.getString("username").trim().eqauls("null")

+3
May 14 '12 at 18:16
source share

If you must first parse json and process the object, try this

Parser

 Object data = json.get("username"); 

Handler

 if (data instanceof Integer || data instanceof Double || data instanceof Long) { // handle number ; } else if (data instanceof String) { // hanle string; } else if (data == JSONObject.NULL) { // hanle null; } 
+3
Mar 07 '13 at 10:10
source share

Here is a helper method that I use so that I can get JSON strings with only one line of code:

 public String getJsonString(JSONObject jso, String field) { if(jso.isNull(field)) return null; else try { return jso.getString(field); } catch(Exception ex) { LogHelper.e("model", "Error parsing value"); return null; } } 

and then something like this:

 String mFirstName = getJsonString(jsonObject, "first_name"); 

will give you your string value or safely set your string variable to null. I use Gson whenever I can to avoid such traps. In my opinion, it is better at changing zero values.

+1
Jul 30 '15 at 0:46
source share



All Articles