First Blog:JSON data,about null
To practice my English writing skills, I will try to do this in English…so…
I’ve being working on map functions,I’ve got some map data in JSON,and I need to use map data to match some locations so I can get coordinates of these locations.
After I transform the map data into JSONObject,I got error when I try use DoubleParse. It caused by the null.when I try to fix this,It took me half of the day!!I really need to figuer this out…here’s the test codes.
First,I declare json like this:
String json = "{ \"type\": \"Feature\", \"OBJECTID\": null, \"省份\":, \"規劃路線編\": \"G580\"}";
JSONObject jsonObject = JSONObject.fromObject(json);
compiling error:
net.sf.json.JSONException: Missing value. at character 45 of { “type”: “Feature”, “OBJECTID”: null, “省份”:, “規劃路線編”: “G580”}
this means if json data missing value,it won’t even pass the compling ! so if json data can be transform to JSONObject,every existing keys will contain a value.the only exception will be null,like “OBJECTID” in this case… so next I test following sets…
SET 1,NO KEY:
String json = "{ \"type\": \"Feature\", \"OBJECTID\": null, \"省份\":\"新疆\", \"規劃路線編\": \"G580\"}"; JSONObject jsonObject = JSONObject.fromObject(json); JSONObject noKeyObject = jsonObject.getJSONObject("???"); String noKeyString = jsonObject.getString("???"); if (JSONNull.getInstance().equals(noKeyObject)) { System.out.println("noKeyObject:JSONNull"); } if (noKeyObject == null) { System.out.println("noKeyObject:null"); } if (noKeyObject.isEmpty()) { System.out.println("noKeyObject:empty"); } if (noKeyObject.isNullObject()) { System.out.println("noKeyObject:nullObject"); } if (JSONNull.getInstance().equals(noKeyString)) { System.out.println("noKeyString:JSONNull"); } if (noKeyString == null) { System.out.println("noKeyString:null"); } if ("".equals(noKeyString)) { System.out.println("noKeyString:"); } if (StringUtils.isBlank(noKeyString)) { System.out.println("noKeyString:blank"); } if (StringUtils.isEmpty(noKeyString)) { System.out.println("noKeyString:empty"); } if ("null".equals(noKeyString)) { System.out.println("noKeyString:\"null\""); }
compiling error!!!
net.sf.json.JSONException: JSONObject["???"] not found.
so if json does’t have key,and we try to get an object,it’s fine,but if we try to get an String,it won’t work!also we can not getInt,getJSONArray,etc…only get an object is allowed…
then I change noKeyString to this,and run…
String noKeyString = noKeyObject.toString();
console:
noKeyObject:JSONNull
noKeyObject:empty
noKeyObject:nullObject
noKeyString:JSONNull
noKeyString:“null”
so if jsonObject does’t contains key,it is not null as the map…it’s JSONNull!!! ==null won’t work!!!we can also use the function of it’s own isEmpty and isNullObject…
when you transform it,String for example…
JSONNull is work too.But StringUtils.isBlank doesn’t!!!turns out it is String “null”…
When the keys were dynamic.we should use getJSONObject then transform to other format!!!and use JSONNUll.getInstance.equals to avoid it doesn’t contains the key before we transform! !
SET 2,NO VALUE:
String json = "{ \"type\": \"Feature\", \"OBJECTID\": null, \"省份\":\"新疆\", \"規劃路線編\": \"G580\"}";
JSONObject jsonObject = JSONObject.fromObject(json);
JSONObject nullValueObject = jsonObject.getJSONObject("OBJECTID");
String nullValueString = jsonObject.getString("OBJECTID");
if (JSONNull.getInstance().equals(nullValueObject)) {
System.out.println("nullValueObject:JSONNull");
}
if (nullValueObject == null) {
System.out.println("nullValueObject:null");
}
if (nullValueObject.isEmpty()) {
System.out.println("nullValueObject:empty");
}
if (nullValueObject.isNullObject()) {
System.out.println("nullValueObject:nullObject");
}
if (JSONNull.getInstance().equals(nullValueString)) {
System.out.println("nullValueString:JSONNull");
}
if (nullValueString == null) {
System.out.println("nullValueString:null");
}
if ("".equals(nullValueString)) {
System.out.println("nullValueString:");
}
if (StringUtils.isBlank(nullValueString)) {
System.out.println("nullValueString:blank");
}
if ("null".equals(nullValueString)) {
System.out.println("nullValueString:\"null\"");
}
console:
nullValueObject:JSONNull
nullValueObject:empty
nullValueObject:nullObject
nullValueString:JSONNull
nullValueString:“null”
so if value is null,after transform to JSONObject ,it will become to String “null” instead of null…
if it’s value is null,it’s just like the key doesn’t exists!!except getString can pass the compiling!!!
CONCLUSION:
1.IF KEY DOESN’T EXIST,GET AN JSONOBJECT OTHERWISE IT WILL OCCUR COMPILING ERROR!!!
2.IF KEY DOESN’T EXIST,USE JSONNULL.GETINSTANCE.EQUALS() OR JSONOBJECT.ISEMPTY() OR JSONOBJECT.ISNULLOBJECT() TO TESTIFY IF THE KEY EXISTS!!!
3.IF KEY EXIST,IT WON’T OCCUR COMPILING ERROR!BUT NULL IN JSON IS DIFFERENT AS NULL IN MAP,IN MAP IT MEANS AN NULL POINTER,IN JSON IT MEANS STRING “NULL”,USE JSONNULL.GETINSTANCE.EQUALS() OR “NULL”.EQUALS() TO TESTIFY WHETHER VALUE IS NULL OR NOT!
SO DO NOT EVER USE == NULL / != NULL WHEN YOU DEAL WITH JSON DATA!!!