---
title: "action返回JSON数据结构的做法"
author: "iammonster"
date: "2009-11-02T18:08:01Z"
tags: []
slug: "action-json"
draft: false
lastmod: "2009-11-02T18:08:01Z"
id: 508824
url: "https://jiajun.iteye.com/blog/508824"
---

action返回 JSON数据结构，做个笔记。

您是不是感觉有些没注意到的地方呢？

 

```
    /**
     * 创建AJAX应答数据(JSON数据格式)
     * 
     * @param response
     *            输出
     * @param obj
     *            要封装的对象
     * @return null
     * @throws IOException
     */
    private static ActionForward buildJSONDate(final HttpServletResponse response, final Object obj) throws IOException
    {
       //设置编码
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 设置浏览器不要缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        final PrintWriter out = response.getWriter();

        JsonUtil.toJson(out, obj); //把JSON数据结构写到输出流中

        out.flush();

        return null;
    }

```