日期:2014-05-16 浏览次数:20507 次
function showJSON() { var user = { "username":"andy", "age":20, "info": { "tel": "123456", "cellphone": "98765"}, "address": [ {"city":"beijing","postcode":"222333"}, {"city":"newyork","postcode":"555666"} ] } alert(user.username); alert(user.age); alert(user.info.cellphone); alert(user.address[0].city); alert(user.address[0].postcode); }
Map map = new HashMap(); map.put("name", "xj"); map.put("age", 24); JSONObject json = new JSONObject(map); response.getWriter().println(json); response.getWriter().flush();
function query() { $.ajax( { url : "servlet/Hello", global : false, type : "POST", data : ( { Name : "xiongjin" }), dataType : "html", success : function(data){ var temp = jQuery.parseJSON(data); alert(temp.name+temp.age); } }) }
List list = new ArrayList(); Map map = new HashMap(); map.put("name", "xj"); map.put("age", 24); Map map2 = new HashMap(); map2.put("name", "xj1"); map2.put("age", 241); list.add(map); list.add(map2); JSONArray array = new JSONArray(list); response.getWriter().println(array); response.getWriter().flush();
function query() { $.ajax( { url : "servlet/Hello", global : false, type : "POST", data : ( { Name : "xiongjin" }), dataType : "html", success : function(data){ var temp = jQuery.parseJSON(data); for(var i =0;i<temp.length;i++){ alert(temp[i].name+temp[i].age); } } }) }
//注意bean必须是public类型,或者修改JSONObject public class Person { private String name; private String age; public Person(String name, String age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } } Person per = new Person("熊劲","27"); JSONObject json = new JSONObject(per); response.getWriter().println(json); response.getWriter().flush();
function query() { $.ajax( { url : "servlet/Hello", global : false, type : "POST", data : ( { Name : "xiongjin" }), dataType : "html", success : function(data){ var temp = jQuery.parseJSON(data); alert(temp.name+temp.age); } }) }