gogoWebsite

Java creates a new json array_Java creates a JSON object

Updated to 9 hours ago

Java creates JSON objects

private void getJson(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

PrintWriter out=();//The output stream of servlet can be displayed and parsed directly on the page

// As the name suggests, response is the server's response to the browser. PrintWriter's instance is to output the result to the front-end JSP page;

//For example ("Hello World"), there will be Hello World on the JSP page.

//Get a PrintWriter from HttpServletResponse,

//To give a simple example, you can get a pen through the HttpServletResponse object, and then you can use the () method to write whatever you want to display on the web page.

//Through PrintWrite, output html in stream mode, return to the client, and display it on IE.

//Get a stream object that responds to the client

//Get the PrintWriter stream to output on the client.

//When a Servlet responds, the response information is output to the web page through the out object, and it is automatically closed when the response ends. So it can also be understood as:

//When calling the object (), you get the web page's brush. At this time, you can use this brush to draw anything you want to display on the web page.

JSONObject resultJson=new JSONObject(); //JSONObject is an object form

JSONArray jsonArray=new JSONArray();    //JSONArray is an array form

JSONObject jsonObject1=new JSONObject();

("name", "Zhang San");

("age", 22);

JSONObject scoreObject1=new JSONObject();

("chinese", 90);

("math", 100);

("english", 80);

("score", scoreObject1);

JSONObject jsonObject2=new JSONObject();

("name", "Li Si");

("age", 23);

JSONObject scoreObject2=new JSONObject();

("chinese", 70);

("math", 90);

("english", 90);

("score", scoreObject2);

JSONObject jsonObject3=new JSONObject();

("name", "Wang Wu");

("age", 24);

JSONObject scoreObject3=new JSONObject();

("chinese", 80);

("math", 60);

("english", 90);

("score", scoreObject3);

(jsonObject1);

(jsonObject2);

(jsonObject3);

("students", jsonArray);

(resultJson);

(); //Immediately output the buffer data to the receiver

(); //Close the output stream

}