Java go to JSON------implementation Method Collection-----Very practical Oh __js

Source: Internet
Author: User

Mode one: Asynchronous implementation with Ajax, and the example displayed on the page:

================start===================

--JSP Page---

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >
<script type= "Text/javascript" >
function Create () {
if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
}else if (window. ActiveXObject) {
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP")
}
}
function run (URL) {
Create ();
Xmlhttp.open ("POST", url,true);
Xmlhttp.onreadystatechange=callback;
Xmlhttp.send ();
}
function Ajaxjson () {
Run ("ajj0001bg.do?operation=ajaxforjson&&msg=" +new Date ());
}
function callback () {
if (xmlhttp.readystate==4) {
if (xmlhttp.status==200) {
var Xmldoc=xmlhttp.responsetext;
var data=eval (xmldoc);//parsing JSON objects with the Eval function
var Jsondiv=document.getelementbyid ("Jsondata");
var jsonhtml= "<table border=1 ><tr><td> number </td><td> name </td><td> sex </td ></tr> ";
for (Var i=0;i<data.length;i++) {
jsonhtml=jsonhtml+ "<tr><td>" +data[i].userid+ "</td><td>" +data[i].username+ "</td> <td> "+data[i].usersex+" </td></tr>;
}
jsonhtml=jsonhtml+ "</table>";
jsondiv.innerhtml=jsonhtml;
}else{
Alert ("The AJAX server returned an error.") ");
}
}
}
</script>
<body>
This is Testjoson JSP page. <br>
<input type= "button" value= "Testjson" onclick= "Ajaxjson ()"/><br/>
<font color= "Red" >json data are as follows:</font>
<div id= "Jsondata" ></div>
</body>

----------The background action code----------------
Import net.sf.json.jsonarray;//requires importing this JSON package

public class Ajj0001bgaction extends topaction{

Public Actionforward Testjson (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
Return Mapping.findforward ("Testjson");
}
/**
* Test JSON
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
Public Actionforward Ajaxforjson (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
Manufacturing Fake data
List<usermodel> list=new arraylist<usermodel> ();
Usermodel user1=new Usermodel ()//User Object 1
User1.setuserid (1);
User1.setusername ("haha");
User1.setusersex ("male");
List.add (user1);

Usermodel user2=new Usermodel ()//User Object 2
User2.setuserid (2);
User2.setusername ("hehe");
User2.setusersex ("female");
List.add (User2);
Converting a list to JSON
Jsonarray json=jsonarray.fromobject (list);

SYSTEM.OUT.PRINTLN (JSON);//Output see if it's JSON format
Set encoding
Response.setcharacterencoding ("Utf-8");
Write to the foreground
Response.getwriter (). Write (json.tostring ());

return null;
}

}

----All of the jar packages you need are as follows-----

Json_lib.rar

===============end====================

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.