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"}]