org.json.jar
org.json.jar(java转换)
json.js(js转换)
action:
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response =ServletActionContext.getResponse();
int id=Integer.parseInt(request.getParameter("id"));
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
List<Book> books = new ArrayList<Book>();
Book b1 = new Book();
b1.setId(1);
b1.setName("book1");
Book b2 = new Book();
b2.setId(2);
b2.setName("book2");
books.add(b1);
books.add(b2);
Order order = new Order();
order.setBooks(books);
order.setCost(100.5f);
order.setName("orderName");
order.setDate(new Date(System.currentTimeMillis()));
System.out.println(order.toJSONObject());
out.print(order.toJSONObject());
out.flush();
out.close();
html:
function doTest(){
alert(1);
$.ajax({
type: "post",//get也行 不知道为什么加了就不进action断点
url: "question!toModifQuestion.action",
data: "id=5",
dataType: "json",
success:function(data){
alert(1);
alert(data.name);
alert(data.date);
alert(data.books[0].name);
//var json = eval(data); //eval("(" + data + ")");
//$("#divAAA").empty();
//$("#divAAA").html();
$.each(data, function(idx, item) {
alert(item.books[idx].name);
});
$.each(albumData, function(i, n) {
alert(n.AlbumName);
});
var obj = { one:1, two:2, three:3, four:4};
$.each(obj, function(key, val) {
alert(obj[key]);
});
var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]]
$.each(arr1, function(i, item){
alert(item[0]);
});
var obj = { one:1, two:2, three:3, four:4};
$.each(obj, function(key, val) {
alert(obj[key]);
});
},
error: function(xhr,msg,e) {
alert(e);
}
});
}