用org.json包实现基于Json的Ajax

package demo;

import org.json.JSONException;
import org.json.JSONObject;

public class Book {

	private String name;
	private int id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public JSONObject toJSONObject() throws JSONException{
		JSONObject json = new JSONObject();
		json.put("id", id);
		json.put("name", name);
		return json;
	}
}

package demo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sun.org.apache.bcel.internal.generic.NEW;


public class Order {

	private String name;
	private float cost;
	private Date date;
	private List<Book> books = new ArrayList<Book>();
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getCost() {
		return cost;
	}
	public void setCost(float cost) {
		this.cost = cost;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public List<Book> getBooks() {
		return books;
	}
	public void setBooks(List<Book> books) {
		this.books = books;
	}
	
	public JSONObject toJSONObject() throws JSONException{
		JSONObject json = new JSONObject();
		json.put("name", name);
		json.put("date", new SimpleDateFormat("yyyy-MM-dd").format(date));
		json.put("cost", cost);
		JSONArray jsonBooks = new JSONArray();
		for (Book book : books) {
			jsonBooks.put(book.toJSONObject());
		}
		json.put("books", jsonBooks);
		return json;
	}
}

package demo;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

public class JSONResponse extends HttpServlet {

	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("application/x-json");
		
		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()));
		try {
			response.getWriter().print(order.toJSONObject());
		} catch (JSONException e) {
		}
	}
}

  <script type="text/javascript">
  //req是XMLHttpRequest对象
  var jsonObj = "(" + req.responseText + ")";
  var order = eval(jsonObj);

你可能感兴趣的:(java,apache,Ajax,json,servlet)