Java类中数据以JSON格式输出结果

 

 

 

/**
	 * 以格式输出结果
	 * @param result
	 */
	public void responseJson(boolean result,String msg){
		
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpServletResponse resp = ServletActionContext.getResponse();
		
	    resp.setContentType("text/xml;charset=utf-8");
	    resp.setHeader("Cache-Control","no-cache"); 
	    
		
	    String callback = req.getParameter("callback");
	    JSONObject json = new JSONObject();   

	    try {
	        
	    	//取集合
			//JSONArray jsonArray = JSONArray.fromObject(list);

			JSONObject jsobjcet = new JSONObject();

			jsobjcet.put("result", result);
			jsobjcet.put("msg", msg);

			resp.getWriter().write(callback + "(" + jsobjcet.toString() + ");");

			System.out.println(jsobjcet.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	

 

需要导入的包

import net.sf.json.JSONObject;

 

你可能感兴趣的:(java,.net,json,xml,cache)