从Web services生成JSON输出
既然JSON的首要目标是来自浏览器的信道外请求,那么我们选择REST风格(RESTful)Web服务来生成这些数据。除了用典型业务逻辑探究Web服务之外,还将采用特定的API把本地Java结构转化为JSON格式(详见 参考资料)。首先,下面的Java代码用来操纵Address对象:
// 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); |
该Java结构在哪里生成并不重要(可能是在JSP、Servlet、EJB或POJO中生成),重要的是,在REST风格Web 服务中有权使用这些数据。如下示:
// 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 } |
延伸阅读:
什么是JSON
JSON 入门指南
实例详解PHP serialize与JSON解析
PHP中JSON的应用
PHP中JSON技巧讲解
PHP json_encode函数进行中文转换
JSON Jquery Codeigniter 使用详解
在PHP使用json_encode
jquery JSON的解析方式
jQuery新版本加载json注意事项
jQuery插件—把xml转化为json插件