Installation
To use JSON.simple you need to download the jar and add to your build path. The jar is available here: https://code.google.com/p/json-simple/Creating a Single Object
Document doc = col.getFirstDocument();
JSONObject json = new JSONObject();
if(doc != null){
Name name = session.createName(doc.getItemValueString("EmployeeName"));
json.put("employeeName", name.getCommon());
json.put("employeeID", doc.getItemValueString("EmployeeID"));
json.put("employeeLocation", doc.getItemValueString("locationdesc"));
json.put("employeeRole", getEmployeeRole(name.getCommon()));
}
returnValue = json.toJSONString();
...
return returnValue;
The result will look something like this:
{"employeeLocation":"Pensacola Milton","employeeRole":"Intern to the Intern","employeeID":"12345","employeeName":"Dwain Wuerfel"}
Creating a Collection of Objects
JSONArray result = new JSONArray();
if(nabDoc != null){
Item item = nabDoc.getFirstItem("Members");
Vector<String> v = item.getValues();
for(String person : v){
Name name = session.createName(person);
JSONObject json = new JSONObject();
json.put("employeeName", name.getCommon());
json.put("employeeRole", employeeRole);
json.put("employeeID", EmployeeID);
json.put("employeeLocation", EmployeeLocation);
result.add(json);
}
returnValue = result.toJSONString();
...
return returnValue;
The result will look like this if there are three JSON objects in the array:
[{"employeeLocation":"Pensacola Milton","employeeRole":["Minion"],"employeeID":"12345","employeeName":"Dwain Wuerfel"}, {"employeeLocation":"Pensacola Milton","employeeRole":["Blog Author"],"employeeID":"91984","employeeName":"Steve Zavocki"},{"employeeLocation":"Pensacola Milton","employeeRole":["Cast Member"],"employeeID":"54321","employeeName":"Vernon Miles"}]
This comment has been removed by the author.
ReplyDeleteA question I get asked a lot about my preference of using GSON, why use JSON.simple as opposed to the included com.ibm.commons.util.io.json package? I'm curious as to how they compare, feature set wise, as they seem to generally read about the same (JSONObject vs JsonJavaObject, building an object manually, etc.).
ReplyDeleteCheck the link I posted in my conclusion, it gives a nice comparison test between 4 options. iBM commons is not one of them though.
Delete@Eric I am not aware of solid reasons to use JSON.simple over GSON or Jackson or Boon or Genson -- all of which are (in my opinion) much better libraries for JSON handling. Neither JSON.simple nor org.json library supports anything other than simple tree model; they are not particularly fast, easy to use. If you are happy with GSON I see no reason to consider JSON.simple as an alternative.
ReplyDeleteIt's more of a curiosity, as I'm less familiar with its feature set (added some app maintenance to an app that used it, but that's it). I'm definitely happy with being able to perform object reflection with those solutions that can do so.
DeleteEric, I also think that there is no solid reason to use JSON.simple if you already know GSON or another toolkit. When I developed this I forgot about the IBM commons. I am presenting this as an option, but not claiming it is a better option.
DeleteI kept waiting for you to notice.
ReplyDelete