无法创建新的JSONObject();我哪里错了

时间:2012-03-15 05:38:42

标签: json java-ee gson

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.Gson;

@WebServlet("/GetJSONUser")
public class GetJSONUser extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("##QUERY STRING(Get): "+request.getQueryString());
        PrintWriter writer = response.getWriter();
        Gson json = new Gson();
        ArrayList<User> userList = UserService.GetUserList(getServletContext());
        try{
        JSONObject responseData = new JSONObject();
        JSONArray cellArray = new JSONArray();
        JSONArray cell = new JSONArray();
        JSONObject cellObject = new JSONObject();

        for(int i=0;i<userList.size();i++)
        {
            cellObject.put("id",i+1);

            cell = new JSONArray();
            cell.add(userList.get(i).id);
            cell.add( userList.get(i).username);
            cell.add(userList.get(i).roleId);

            cellObject.put("cell", cell);
            cellArray.add(cellObject);
        }

        responseData.put("page", 1);
        responseData.put("total", 1);
        responseData.put("records ", userList.size());
        responseData.put("rows", cellObject);
        String jsonReturn = json.toJson(responseData);
        System.out.println(jsonReturn);
        writer.print(jsonReturn);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("##QUERY STRING: "+request.getParameterMap());
        PrintWriter writer = response.getWriter();
        Gson json = new Gson();
        ArrayList<User> userList = UserService.GetUserList(getServletContext());
        String jsonReturn = json.toJson(userList);
        System.out.println(jsonReturn);
        writer.print(jsonReturn);
    }
}

错误:

未找到来源

附加源不包含文件ApplicationFilterChain.class的源代码

当它到达

行时

JSONObject responseData = new JSONObject();

enter image description here

Gson.jar和json-lib-2.4-jdk15.jar都在lib文件夹中

1 个答案:

答案 0 :(得分:0)

而不是

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.JsonObject;
import com.google.gson.JsonArray;