文章来源http://www.goomoon.com/it/web/js/7960.html
虽说是springMVC的暂时可以参考一下
JSON(JavaScriptObject Notation)是一种轻量级的数据交换格式。简单地说,JSON可以将JavaScript对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从Web客户机传递给服务器端程序。这个字符串看起来有点儿古怪,但是JavaScript很容易解释它,而且JSON可以表示比"名称/值对"更复杂的结构。例如,可以表示数组和复杂的对象,而不仅仅是键和值的简单列表。
下面举几个简单的实例来介绍Json,因为JSON是javascript的原生格式,因此不需要任何特殊的API包或工具包。
实例一
:
01 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" |
02 | pageEncoding="ISO-8859-1"%> |
06 | < meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" > |
07 | < title >Insert title here</ title > |
08 | < script type = "text/javascript" > |
15 | "info":{"tel":"lf","cellphone":"1223"}, |
18 | {"city":"lf","postcode":"111"}, |
19 | {"city":"bj","postcode":"222"}, |
24 | alert(user.info.cellphone); |
25 | alert(user.address[1].postcode); |
30 | < input type = "button" value = "TestJson" onclick = "testJson()" > |
|
可直接运行测试。
实例二
:
下面这个实例是前后台如何交互的实例,首先把环境搭起来,创建一个Maven项目json_test,具体创建过程不再赘余,pom.xml文件:
03 | < modelVersion >4.0.0</ modelVersion > |
04 | < groupId >com.tgb</ groupId > |
05 | < artifactId >json_test</ artifactId > |
06 | < packaging >war</ packaging > |
07 | < version >0.0.1-SNAPSHOT</ version > |
08 | < name >json_test Maven Webapp</ name > |
12 | < groupId >junit</ groupId > |
13 | < artifactId >junit</ artifactId > |
14 | < version >4.0</ version > |
18 | < groupId >commons-lang</ groupId > |
19 | < artifactId >commons-lang</ artifactId > |
20 | < version >2.4</ version > |
23 | < groupId >commons-beanutils</ groupId > |
24 | < artifactId >commons-beanutils</ artifactId > |
25 | < version >1.7.0</ version > |
28 | < groupId >commons-collections</ groupId > |
29 | < artifactId >commons-collections</ artifactId > |
30 | < version >3.2</ version > |
33 | < groupId >commons-logging</ groupId > |
34 | < artifactId >commons-logging</ artifactId > |
35 | < version >1.1.1</ version > |
38 | < groupId >net.sf.ezmorph</ groupId > |
39 | < artifactId >ezmorph</ artifactId > |
40 | < version >1.0.6</ version > |
43 | < groupId >net.sf.json-lib</ groupId > |
44 | < artifactId >json-lib</ artifactId > |
45 | < version >2.4</ version > |
46 | < classifier >jdk15</ classifier > |
49 | < groupId >org.springframework</ groupId > |
50 | < artifactId >spring-webmvc</ artifactId > |
51 | < version >3.0.4.RELEASE</ version > |
56 | < finalName >json_test</ finalName > |
|
Web.xml
配置如下
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | < display-name >json_test</ display-name > |
05 | < welcome-file >index.jsp</ welcome-file > |
09 | < servlet-name >springMVC</ servlet-name > |
10 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
11 | < load-on-startup >1</ load-on-startup > |
15 | < servlet-name >springMVC</ servlet-name > |
16 | < url-pattern >/</ url-pattern > |
|
SpringMVC
的配置文件和
web.xml
文件在同一个目录下,
springMVC-servlet.xml
:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
13 | < context:component-scan base-package = "com.tgb.json" /> |
15 | < mvc:annotation-driven /> |
17 | < mvc:resources location = "/js/" mapping = "/js/**" /> |
|
上面关于SpringMVC的东西也不多讲,因为这不是要讲的重点。项目中还会用到Jquery,因此大家自行下载jquery包,放到webapp/js包下。
下面开始编写后台的代码,首先创建一个POJO类User:
05 | private String username; |
09 | public String getUsername() { |
13 | public void setUsername(String username) { |
14 | this .username = username; |
21 | public void setAge( int age) { |
|
然后创建
TestJson
类,在这个类中,我写了三个方法,是分别测试传递
User
对象、
List
集合和
Map
的。
02 | import java.io.PrintWriter; |
03 | import java.util.ArrayList; |
04 | import java.util.HashMap; |
05 | import java.util.List; |
08 | import javax.servlet.http.HttpServletRequest; |
09 | import javax.servlet.http.HttpServletResponse; |
11 | import net.sf.json.JSONArray; |
12 | import net.sf.json.JSONObject; |
14 | import org.springframework.stereotype.Controller; |
15 | import org.springframework.web.bind.annotation.RequestMapping; |
18 | public class TestJson { |
20 | @RequestMapping ( "logon.do" ) |
21 | public String logon(HttpServletRequest request,HttpServletResponse response){ |
22 | return "/test/addUser" ; |
26 | @RequestMapping ( "/test/json1" ) |
27 | public void returnUser(HttpServletRequest request,HttpServletResponse response) throws Exception{ |
29 | response.setContentType( "text/html" ); |
30 | PrintWriter out = response.getWriter(); |
33 | u1.setUsername( "tom1" ); |
37 | JSONObject jsonObject = JSONObject.fromObject(u1); |
39 | out.print(jsonObject); |
43 | @RequestMapping ( "/test/json2" ) |
44 | public void returnList(HttpServletRequest request,HttpServletResponse response) throws Exception{ |
46 | response.setContentType( "text/html" ); |
47 | PrintWriter out = response.getWriter(); |
50 | List<User> list = new ArrayList<User>(); |
53 | u1.setUsername( "tom1" ); |
57 | u2.setUsername( "jack" ); |
63 | JSONArray jsonArray = JSONArray.fromObject(list); |
69 | @RequestMapping ( "/test/json3" ) |
70 | public void returnMap(HttpServletRequest request,HttpServletResponse response) throws Exception{ |
72 | response.setContentType( "text/html" ); |
73 | PrintWriter out = response.getWriter(); |
75 | Map<String,User> map = new HashMap<String,User>(); |
78 | u1.setUsername( "tom1" ); |
82 | u2.setUsername( "jack" ); |
89 | JSONObject jsonObject = JSONObject.fromObject(map); |
91 | out.print(jsonObject); |
|
完成了后台代码的编写,页面上就需要获取后台传过来的
Json
数据,并将它显示出来,创建页面
testJson.jsp
:
01 | <%@ page language="java" contentType="text/html; charset=utf-8" |
02 | pageEncoding="utf-8"%> |
06 | < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" > |
07 | < script type = "text/javascript" src = "json_test/../js/jquery-1.7.1.js" ></ script > |
08 | < title >Insert title here</ title > |
09 | < script type = "text/javascript" > |
12 | function getJsonObject(){ |
15 | url:"/json_test/test/json1", |
17 | success:function(json){ |
28 | function getJsonList(){ |
31 | url:"/json_test/test/json2", |
33 | success:function(json){ |
34 | alert(json[0].username); |
44 | function getJsonMap(){ |
47 | url:"/json_test/test/json3", |
49 | success:function(json){ |
50 | alert(json.u1.username); |
61 | < input type = "button" value = "获取User对象" onclick = "getJsonObject()" >< br /> |
62 | < input type = "button" value = "获取List" onclick = "getJsonList()" >< br /> |
63 | < input type = "button" value = "获取Map" onclick = "getJsonMap()" > |
|
通过以上这几个例子基本能说明
Json
的大致用途,他和
XML
的联系与区别,大家可自行查阅。