⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsonexample.java

📁 ajax基础教程配套的例子源代码
💻 JAVA
字号:
package ajaxbook.chap3;import java.io.*;import java.net.*;import java.text.ParseException;import javax.servlet.*;import javax.servlet.http.*;import org.json.JSONObject;public class JSONExample extends HttpServlet {        protected void doPost(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {        String json = readJSONStringFromRequestBody(request);                //Use the JSON-Java binding library to create a JSON object in Java        JSONObject jsonObject = null;        try {            jsonObject = new JSONObject(json);        }        catch(ParseException pe) {            System.out.println("ParseException: " + pe.toString());        }                String responseText = "You have a " + jsonObject.getInt("year") + " "            + jsonObject.getString("make") + " " + jsonObject.getString("model")            + " " + " that is " + jsonObject.getString("color") + " in color.";                response.setContentType("text/xml");        response.getWriter().print(responseText);    }    private String readJSONStringFromRequestBody(HttpServletRequest request){        StringBuffer json = new StringBuffer();        String line = null;        try {            BufferedReader reader = request.getReader();            while((line = reader.readLine()) != null) {                json.append(line);            }        }        catch(Exception e) {            System.out.println("Error reading JSON string: " + e.toString());        }        return json.toString();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -