JavaScript:
----------array: object
定义: var obj= [1, 2, 3, 4]; // 等价于 var obj= new Array(1, 2, 3, 4);
----------Json: object
js里面,有一种特殊的对象,就是Json,可以看做一种特殊的Map(仅方便联想)
var data = {"uid":"uuid","username":"xt","sex":"男","password":"123456"};
-------------基于对象的使用: data.uuid==========>data["uuid"] 前台去处理Json
----------------------------------------------------------->ajax使用
传 json 得到json
controller-------------->前台界面(显示数据) ===================》局部刷新界面,而不是整个界面
Ajax操作分为四步走:两种请求方式 post,get
1)获取XMLHttpRequest
fucntion craeteXMLHttpRequest(){
try{
return new XMLHttpRequest();
}catch(e){
try{
return new ActiveXObject("Microsoft.XMLHTTP")
}catch(e){
try{
return new ActiveXObject("msxml2.XMLHTTP")
}catch(e){
throw e;
}
}
}
var xmlHttpRequest = new craeteXMLHttpRequest();
2)打开窗口(如咱们一般访问浏览器)
xmlHttpRequest.open("Get","<c:url value='/xt?action=hello&xt=aaa'/>",true); ------》get
xmlHttpRequest.open("POST","<c:url value='/product.do?action=checkByPid'/>",true); ---->post 要设置请求头
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
<c:url value='/xt?action=hello&xt=aaa'/> ------------->也可以是一般文本文件
3)浏览器里面输入数据(send)
xmlHttpRequest.send(null)------------->get
xmlHttpRequest.send("username"+username)------------->post
4)浏览器回应
xmlHttpRequest.onreadystatechange = function(){
if(xmlHttpRequest.readyState==4 &&xmlHttpRequest.status == 200){
var text = xmlHttpRequest.responseText; //类型text,一般可以接收str
var data = JSON.parse(text); // eval("(" +data+ ")") 两种解析方式
var xml = xmlHttpRequest.responseXML; //xml str为xml形式
};
后台 bean------------》json给前台
1 package cn.transfer.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.TreeMap; 7 8 import net.sf.json.JSONArray; 9 import net.sf.json.JSONObject; 10 11 import org.junit.Test; 12 13 public class TestJson { 14 /* 15 * 当map来用 16 */ 17 @Test 18 public void fun1() { 19 JSONObject map = new JSONObject(); 20 map.put("name", "zhangSan"); 21 map.put("age", 23); 22 map.put("sex", "male"); 23 24 String s = map.toString(); 25 System.out.println(s); 26 27 28 System.out.println("----------------------------"); 29 Map<String,Object> map2 = new TreeMap<String, Object>(); 30 map2.put("age", 12);map2.put("name", "LiSi");map2.put("sex", "男"); 31 System.out.println(map2.toString().replace("=", ":"));//{sex=男, name=LiSi, age=12} 32 } 33 34 @Test 35 public void beanToJson(){ 36 Person person = new Person("LiSi", 12, "男"); 37 //对象转换为json 38 JSONObject map = JSONObject.fromObject(person); 39 System.out.println(map.toString()); 40 } 41 42 /** 43 * JSONArray 44 */ 45 @Test 46 public void fun3() { 47 Person p1 = new Person("zhangSan", 23, "male"); 48 Person p2 = new Person("liSi", 32, "female"); 49 50 JSONArray list = new JSONArray(); 51 list.add(p1); 52 list.add(p2); 53 54 System.out.println(list.toString()); 55 } 56 57 /** 58 * 原来就有一个List,我们需要把List转换成JSONArray 59 */ 60 @Test 61 public void fun4() { 62 Person p1 = new Person("zhangSan", 23, "male"); 63 Person p2 = new Person("liSi", 32, "female"); 64 List<Person> list = new ArrayList<Person>(); 65 list.add(p1); 66 list.add(p2); 67 68 System.out.println(JSONArray.fromObject(list).toString()); 69 } 70 71 @Test // 数组--------------<>JSON 72 public void fun5() { 73 Person p1 = new Person("zhangSan", 27773, "male"); 74 Person p2 = new Person("liSi", 32, "female"); 75 Person[] p = {p1,p2}; 76 77 System.out.println(JSONArray.fromObject(p).toString()); 78 } 79 80 @Test // String------------>JSON 81 public void fun6() { 82 83 84 JSONObject map = new JSONObject(); 85 map.put("name", "zhangSan"); 86 map.put("age", 23); 87 map.put("sex", "male"); 88 Person obj = (Person) JSONObject.toBean(map, Person.class); 89 System.out.println(obj.toString() ); 90 } 91 }