服務器端返回json

服務器端讀取數據庫 返回list 然後將list打包成json
    public String loadModel() throws DataBaseException, IOException{
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpServletResponse response=ServletActionContext.getResponse();
        
        response.setContentType("application/x-javascript;charset=UTF-8");  
        PrintWriter out = response.getWriter();
        
        String brandId = request.getParameter("brandId");
        List phones = this.phoneService.queryMobilByBrandId(Integer.parseInt(brandId));
        if(phones!=null&&phones.size()>0){
            //把集合對象轉換爲json對象
            JSONArray json=JSONArray.fromObject(phones);
            out.println(json.toString());
            out.close();
        }else{
            out.println("[{id:0,model:'請選擇'}]");        
            out.close();
        }
        return SUCCESS;
    }

發佈了33 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章