// Create addressbook data structure SortedMap addressBook = new TreeMap(); // Create new address entries and place in Map // (See download for Address POJO structure) Address maryLebow = new Address("5 Main Street","San Diego, CA",91912,"619-332-3452","664-223-4667"); addressBook.put("Mary Lebow",maryLebow); Address amySmith = new Address("25 H Street","Los Angeles, CA",95212,"660-332-3452","541-223-4667"); addressBook.put("Sally May",amySmith); Address johnKim = new Address("2343 Sugarland Drive","Houston, TX",55212,"554-332-3412","461-223-4667"); addressBook.put("John Kim",johnKim); Address richardThorn = new Address("14 68th Street","New York, NY",,12452,"212-132-6182","161-923-4001"); addressBook.put("Richard Thorn",richardThorn);
// Define placeholder for JSON response String result = new String(); // Get parameter (if any) passed into application String from = request.getParameter("from"); String to = request.getParameter("to"); try { // Check for parameters, if passed filter address book if(from != null && to != null) { // Filter address book by initial addressBook = addressBook.subMap(from,to); } // Prepare the convert addressBook Map to JSON array // Array used to place numerous address entries JSONArray jsonAddressBook = new JSONArray(); // Iterate over filtered addressBook entries for (Iterator iter = addressBook.entrySet().iterator(); iter.hasNext() { // Get entry for current iteration Map.Entry entry = (Map.Entry)iter.next(); String key = (String)entry.getKey(); Address addressValue = (Address)entry.getValue(); // Place entry with key value assigned to "name" JSONObject jsonResult = new JSONObject(); jsonResult.put("name",key); // Get and create address structure corresponding to each key // appending address entry in JSON format to result String streetText = addressValue.getStreet(); String cityText = addressValue.getCity(); int zipText = addressValue.getZip(); JSONObject jsonAddress = new JSONObject(); jsonAddress.append("street",streetText); jsonAddress.append("city",cityText); jsonAddress.append("zip",zipText); jsonResult.put("address",jsonAddress); // Get and create telephone structure corresponding to each key // appending telephone entries in JSON format to result String telText = addressValue.getTel(); String telTwoText = addressValue.getTelTwo(); JSONArray jsonTelephones = new JSONArray(); jsonTelephones.put(telText); jsonTelephones.put(telTwoText); jsonResult.put("phoneNumbers",jsonTelephones); // Place JSON address entry in global jsonAddressBook jsonAddressBook.put(jsonResult); } // end loop over address book // Assign JSON address book to result String result = new JSONObject().put("addressbook",jsonAddressBook).toString(); } catch (Exception e) { // Error occurred }
<html> <head> <title> JSON Address Book </title> <script type="text/javascript" src="prototype-1.4.0.js"></script> <script type="text/javascript"> // Method invoked when user changes letter range function searchAddressBook() { // Select values from HTML select lists var fromLetter = $F('fromLetter'); var toLetter = = $F('toLetter'); // Prepare parameters to send into REST web service var pars = 'from=' + fromLetter + '&to=' + toLetter; // Define REST web service URL var url = 'restservice.jsp'; // Make web service Ajax request via prototype helper, // upon response, call showResponse method new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: showResponse }); } </script> </head>