Posted on 2008-12-26 19:12
H2O 阅读(679)
评论(0) 编辑 收藏 所属分类:
ajax
什么是JSON?
一种轻量级的数据传送格式,{}对象,[]数组,"属性名":"属性值",用逗号隔开。废话少说,看代码更直接,以下自己写的例子。
<script type="text/javascript">
//纯字符串+eval
define = "{}{name:'Michael',email:'17bity@gmail.com'}";
eval("data = "+define);
alert("name:"+data.name);
alert("email:"+data.email);
//纯对象
var user ={}{
"name":"yizhi",
"age":22,
"belive":"阳光总在风雨后"
};
alert(user.name);
alert(user.age);
alert(user.belive);
//纯数组
var color =
[
{}{"colorName":"red","symbol":"喜庆"},
{}{"colorName":"green","symbol":"希望"}
]
;
alert(color[0].colorName+"------>"+color[0].symbol);
alert(color[1].colorName+"------>"+color[1].symbol);
//对象+属性+数组+属性
var jsonObj ={}{
"myProperty":"我的属性",
"myObject":{
"name":{
"firstName":"易",
"familyName":"志",
"fullName":"易志"
},
"age":"myObject的年龄"
},
"myFriends":[
{}{
"name":"我的第①个朋友的名字",
"age":"我的第①个朋友的年龄"
},
{}{
"name":"我的第②个朋友的名字",
"age":"我的第②个朋友的年龄"
}
]
}
alert("jsonObje----------->复杂的json对象");
alert(jsonObj.myProperty);
alert(jsonObj.myObject.name.firstName);
alert(jsonObj.myObject.name.familyName);
alert(jsonObj.myObject.name.fullName);
alert(jsonObj.myObject.age);
alert(jsonObj.myFriends[0].name);
alert(jsonObj.myFriends[0].age);
alert(jsonObj.myFriends[1].name);
alert(jsonObj.myFriends[1].age);
</script>
可通过ajax把json字符串传到后台 后台可以把json字符串转为对象,也可以把对象或者数组转为json字符串,必须天就爱json的jar包,小测试:
public class User extends ActionForm{
private int id;
private String name;
.
package com.yz.jsonTest;
import java.util.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
public class JsonTest {
public static void main(String[] args) throws JSONException {
Collection c = getList();
//数组
JSONArray jary = new JSONArray(c);
System.out.println(jary); //[0,1,2,3,4]
String jsonStr2 ="[{'a':'1','b':'2'},{'a':'3','b':'4'}]";
JSONArray jary2 = new JSONArray(jsonStr2);
System.out.println(jary2.getJSONObject(1).get("a")); //3
//对象
String jsonStr="{'name':'yizhi','age':'19'}";
JSONObject jobj = new JSONObject(jsonStr);
System.out.println(jobj.get("name")); //yizhi
System.out.println(jobj.get("age")); //19
Map mp = new HashMap();
mp.put("happy", "开心");
mp.put("sad", "悲伤");
JSONObject jobj2 = new JSONObject(mp);
System.out.println(jobj2.get("happy")); //开心
}
public static List getList(){
List lst = new ArrayList();
for (int i = 0; i < 5; i++) {
lst.add(i);
}
return lst;
}
}
Action中。。
package com.yz.jsonTest;
import java.util.ArrayList;
import java.util.List;
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 org.json.JSONArray;
public class JsonTestAction extends DispatchAction {
public List<User> getUserList(){
List<User> lst = new ArrayList<User>();
for(int i=0;i<3;i++){
User u = new User();
u.setId(i);
u.setAge(i+20);
u.setName(i+"号");
lst.add(u);
}
return lst;
}
public ActionForward TestJson(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
JSONArray jary = new JSONArray(getUserList());
System.out.println(jary);
return null;
}
}