2009-11-14
JSON instance (ajax+struts)
JSON: There are JSON arrays and JSON entities
JSON entity: {key-value pairs}
JSON array: [{},{},{}]
Eval:
Var json object =eval (' (' JSON data ') ');
The bracketed content is returned by Eval () with a JSON object that is an array (an array is actually an object), which has 4 data, and the 4 data is an array type, with 5 data inside each data
JS in how to parse it ...
If the content returned above is placed in a variable of content, you can get the object by using the following method
var json = eval (' + content + '); Note that the brackets on both sides of this can not be removed, because in JS each method can be used as a class to generate objects, here is a simple way to generate the JSON object.
How to get information about an object.
var info = json[0]; obtained is ["122747439", "Peach Blossom Tears", "male", "22", "1987-2-17 0:00:00"]
var info_qqid = json[0][0]; gets "122747439"
This is accomplished by using an array.
JS Code:
var d = "(" + Data + ")";
var person = eval (d);
var div = "<div>";
var test = person[' info '];
var ab = Test[0].qqname;
var info = person[' info '];
for (var o in info) {
var span = "<span>";
span + = info[o][' qqid ' + " | ";
Span + + Info[o].qqname + " | ";
Span + + Info[o].sex + " | ";
Span + + info[o].age + " | ";
Span + + Info[o].birthday + " | ";
span + = "</span>";
div = + span + "<br/>";
}
div = + "</div>";
$ (' #data '). html (div);
Some time ago to do the project to use JSON, today I took the time to write a struts+ajax+json example.
Personal feeling ajax+json to a great extent reduces the network and server IO, is a very good combination.
1:json's lib I'm using Json-lib-2.1-jdk15.jar, it can be in
2:struts is using 1.2.
3: Used to the JS Third-party prototype.js, mainly with its packaging of Ajax objects, we do not need to use this, you can directly in JS with XMLHttpRequest.
The following are the relevant files used in the example:
Java code
Toolhxw.js
/**
@hxw 20080602
*/
callback function Simple callback function
function Showesay (dataresponse)
{
var data = eval (' (' + dataresponse.responsetext + ') ');
var str= ';
str+= ' <ul> ';
str+= ' <li> ' +data.param1;+ ' </li> ';
str+= ' <li> ' +data.param2;+ ' </li> ';
str+= ' </ul> ';
document.getElementById ("Content"). Innerhtml=str;
}
callback function Complex callback function
function Showcomplex (dataresponse)
{
var data = eval (' (' + dataresponse.responsetext + ') ');
var str= ';
for (Var i=0;i<data.js.length;i++)
{
str+= ' <ul> ';
str+= ' <li> ' +data.js[i].id+ ' </li> ';
str+= ' <li> ' +data.js[i].age+ ' </li> ';
str+= ' <li> ' +data.js[i].name+ ' </li> ';
str+= ' <li> ' +data.js[i].address+ ' </li> ';
str+= ' </ul> ';
}
document.getElementById ("Content"). Innerhtml=str;
}
Get simple JSON data
function Getesay () {
var url = ' test.do ';
var pars = ' method=geteasy ';
var ajax = new Ajax.request (
Url
{method: ' Post ', Parameters:pars,oncomplete:showesay}
);
}
Get object-level complex data
function Getcomplex () {
var url = ' test.do ';
var pars = ' Method=getcomplex ';
var ajax = new Ajax.request (
Url
{method: ' Post ', Parameters:pars,oncomplete:showcomplex}
);
}
Struts-config.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts-config Public "-//apache Software foundation//dtd struts Configuration 1.2//en" "http:// Struts.apache.org/dtds/struts-config_1_2.dtd ">
<struts-config>
<data-sources/>
<form-beans/>
<global-exceptions/>
<global-forwards/>
<action-mappings >
<action path= "/test" parameter= "method" Type= "Com.json.struts.action.TestAction" >
</action>
</action-mappings>
<message-resources parameter= "Com.json.struts.ApplicationResources"/>
</struts-config>
Testaction.java
/*
* Generated by MyEclipse Struts
* Template PATH:TEMPLATES/JAVA/JAVACLASS.VTL
*/
Package com.json.struts.action;
Import Java.io.PrintWriter;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
Import org.apache.struts.actions.DispatchAction;
Import net.sf.json.*;
/**
* @author HxW
*
*/
public class Testaction extends Dispatchaction {
/**
* Get simple combination data
*/
Public Actionforward Geteasy (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
Response.setcontenttype ("text/html; CHARSET=GBK ");
Try
{
PrintWriter out = Response.getwriter ();
The data assembly here is typically from a database query.
Jsonobject jsonobject = new Jsonobject ();
Jsonobject.put ("param1", "Variable One");
Jsonobject.put ("param2", "Variable Two");
Out.print (Jsonobject.tostring ());
Out.flush ();
Out.close ();
return null;
}catch (Exception e)
{
E.printstacktrace ();
return null;
}
}
/**
* Get complex combination data
*/
Public Actionforward Getcomplex (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
Response.setcontenttype ("text/html; CHARSET=GBK ");
Try
{
PrintWriter out = Response.getwriter ();
Jsonobject obj = new Jsonobject ();
Jsonarray js = new Jsonarray ();
The data assembly here is typically from a database query.
for (int i=0;i<3;i++)
{
Jsonobject objtemp = new Jsonobject ();
Objtemp.put ("id", i);
Objtemp.put ("Age", "23");
Objtemp.put ("name", "Test" +i);
Objtemp.put ("Address", "test");
Js.add (objtemp);
}
Obj.put ("JS", JS);
Out.print (Obj.tostring ());
}catch (Exception e)
{
E.printstacktrace ();
System.out.print ("Consumption detail JSON storage exception");
}
return null;
}
}
test.jsp
<%@ page language= "java" import= "java.util.*" contenttype= "TEXT/HTML;CHARSET=GBK"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>json Practice </title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<script type= "Text/javascript" src= "Js/prototype.js" ></script>
<script type= "Text/javascript" src= "Js/toolhxw.js" ></script>
<body>
<div id= "Func" >
<a href= ' Javascript:getesay () ' > Get a simple combination of data </a>
<a href= ' Javascript:getcomplex () ' > Get complex combination data </a>
</div>
<div id= "Content" >
Getting content ...
</div>
</body>
Toolhxw.js
/**
@hxw 20080602
*/
callback function Simple callback function
function Showesay (dataresponse)
{
var data = eval (' (' + dataresponse.responsetext + ') ');
var str= ';
str+= ' <ul> ';
str+= ' <li> ' +data.param1;+ ' </li> ';
str+= ' <li> ' +data.param2;+ ' </li> ';
str+= ' </ul> ';
document.getElementById ("Content"). Innerhtml=str;
}
callback function Complex callback function
function Showcomplex (dataresponse)
{
var data = eval (' (' + dataresponse.responsetext + ') ');
var str= ';
for (Var i=0;i<data.js.length;i++)
{
str+= ' <ul> ';
str+= ' <li> ' +data.js[i].id+ ' </li> ';
str+= ' <li> ' +data.js[i].age+ ' </li> ';
str+= ' <li> ' +data.js[i].name+ ' </li> ';
str+= ' <li> ' +data.js[i].address+ ' </li> ';
str+= ' </ul> ';
}
document.getElementById ("Content"). Innerhtml=str;
}
Get simple JSON data
function Getesay () {
var url = ' test.do ';
var pars = ' method=geteasy ';
var ajax = new Ajax.request (
Url
{method: ' Post ', Parameters:pars,oncomplete:showesay}
);
}
Get object-level complex data
function Getcomplex () {
var url = ' test.do ';
var pars = ' Method=getcomplex ';
var ajax = new Ajax.request (
Url
{method: ' Post ', Parameters:pars,oncomplete:showcomplex}
);
}
Struts-config.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts-config Public "-//apache Software foundation//dtd struts Configuration 1.2//en" "http:// Struts.apache.org/dtds/struts-config_1_2.dtd ">
<struts-config>
<data-sources/>
<form-beans/>
<global-exceptions/>
<global-forwards/>
<action-mappings >
<action path= "/test" parameter= "method" Type= "Com.json.struts.action.TestAction" >
</action>
</action-mappings>
<message-resources parameter= "Com.json.struts.ApplicationResources"/>
</struts-config>
Testaction.java
/*
* Generated by MyEclipse Struts
* Template PATH:TEMPLATES/JAVA/JAVACLASS.VTL
*/
Package com.json.struts.action;
Import Java.io.PrintWriter;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
Import org.apache.struts.actions.DispatchAction;
Import net.sf.json.*;
/**
* @author HxW
*
*/
public class Testaction extends Dispatchaction {
/**
* Get simple combination data
*/
Public Actionforward Geteasy (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
Response.setcontenttype ("text/html; CHARSET=GBK ");
Try
{
PrintWriter out = Response.getwriter ();
The data assembly here is typically from a database query.
Jsonobject jsonobject = new Jsonobject ();
Jsonobject.put ("param1", "Variable One");
Jsonobject.put ("param2", "Variable Two");
Out.print (Jsonobject.tostring ());
Out.flush ();
Out.close ();
return null;
}catch (Exception e)
{
E.printstacktrace ();
return null;
}
}
/**
* Get complex combination data
*/
Public Actionforward Getcomplex (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
Response.setcontenttype ("text/html; CHARSET=GBK ");
Try
{
PrintWriter out = Response.getwriter ();
Jsonobject obj = new Jsonobject ();
Jsonarray js = new Jsonarray ();
The data assembly here is typically from a database query.
for (int i=0;i<3;i++)
{
Jsonobject objtemp = new Jsonobject ();
Objtemp.put ("id", i);
Objtemp.put ("Age", "23");
Objtemp.put ("name", "Test" +i);
Objtemp.put ("Address", "test");
Js.add (objtemp);
}
Obj.put ("JS", JS);
Out.print (Obj.tostring ());
}catch (Exception e)
{
E.printstacktrace ();
System.out.print ("Consumption detail JSON storage exception");
}
return null;
}
}
test.jsp
<%@ page language= "java" import= "java.util.*" contenttype= "TEXT/HTML;CHARSET=GBK"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>json Practice </title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<script type= "Text/javascript" src= "Js/prototype.js" ></script>
<script type= "Text/javascript" src= "Js/toolhxw.js" ></script>
<body>
<div id= "Func" >
<a href= ' Javascript:getesay () ' > Get a simple combination of data </a>
<a href= ' Javascript:getcomplex () ' > Get complex combination data </a>
</div>
<div id= "Content" >
Getting content ...
</div>
</body>