miércoles, 20 de marzo de 2013

Clase que escribe json en struts 1.x

Requiere la librería json http://code.google.com/p/google-gson/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;


public class JsonUtil {
    public static void escribirJSON(HttpServletResponse response,Object object){
     Gson gson = new Gson();
  response.addHeader("Content-Type", "text/html; charset=utf-8"); 
  try {
             response.getOutputStream().write(gson.toJson(object).getBytes("UTF-8"));
             response.getOutputStream().flush();
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
    

No hay comentarios:

Publicar un comentario