1.导入阿里巴巴fastjson包。

  <!-- fastJson将对象转化为Json对象 -->
  <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.1.24</version>
  </dependency>

2.写一个公共的Action类,调用jar包中的toJSONStringWithDateFormat方法,其他Action类只要继承该类,就可以调用writeJson方法。

  public class BaseAction {

    public void writeJson(Object object){
      try {
          String json=JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");

          ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");

          ServletActionContext.getResponse().getWriter().write(json);

          ServletActionContext.getResponse().getWriter().flush();

          ServletActionContext.getResponse().getWriter().close();
        }

      catch (IOException e) {

          e.printStackTrace();
        }
    }
}

3.前台收到json格式的返回值,使用jQuery.parseJSON(data)方法将json格式转化为javascript对象;

快速将对象转化为JSON格式的更多相关文章

  1. $.each遍历json对象(java将对象转化为json格式以及将json解析为普通对象)

    查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1","tagName": ...

  2. Newtonsoft.Json 将C#对象转化为json格式

    object To json public static string ObjectToJson(object obj) { JsonSerializerSettings jss = new Json ...

  3. 把java对象转化为json格式的对象数组

  4. C# 将对象序列化为Json格式

    public static string JsonSerializer<T>(T t) { DataContractJsonSerializer ser = new DataContrac ...

  5. 获取一个表单字段中多条数据并转化为json格式

    如图需要获取下面两个li标签里面的数据,然后传给后台:而后台接收的数据格式是json的,所以需要把两个li里面的信息转化为以下格式的. {recieverName:小红,recieverPhone:1 ...

  6. 一般处理程序中 C#中对象转化为Json对象

    namespace: Newtonsoft.Json; context.Response.ContentType = "application/text"; 注:这里为什么不是 J ...

  7. LINQ查询返回DataTable类型[轉]與将DataTable序列化为Json格式【轉】

    (原文地址:http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/) LINQ查询返回DataTable类型 在使 ...

  8. js中JSON的解析(将json字符串转化为对象)和序列化(将对象转化为json字符串)(函数的功能一般都挺全的,需要的时候去查看完整函数)

    js中JSON的解析(将json字符串转化为对象)和序列化(将对象转化为json字符串)(函数的功能一般都挺全的,需要的时候去查看完整函数) 一.总结 1.JSON解析:JSON.parse(myJS ...

  9. 将网址url中的参数转化为JSON格式的两种方法

    在我们进入主题前,我先先看下获取网址URL的方法: window.location.href // 设置或获取整个URL为字符串 window.location.hash // 设置或获取href属性 ...

随机推荐

  1. JZOJ.5231【NOIP2017模拟8.5】序列问题

    Description   Input 输入文件名为seq.in.首先输入n.接下来输入n个数,描述序列 A. Output 输出文件名为seq.out.输出一行一个整数代表答案.   Sample ...

  2. Spring中 PROPAGATION_REQUIRED 解释

    转自:https://blog.csdn.net/bigtree_3721/article/details/53966617 事务传播行为种类 Spring在TransactionDefinition ...

  3. Android 短信箱操作

    package blackice.android.product; import java.sql.Date; import java.text.SimpleDateFormat; import ja ...

  4. Nginx-rtmp直播之业务流程分析 http://www.mamicode.com/info-detail-2287896.html

    Nginx-rtmp直播之业务流程分析 http://www.mamicode.com/info-detail-2287896.html

  5. python生成百分数

    >>> a = float(5.69875) >>> b = float(8.49385) >>> print a/b 0.67092661160 ...

  6. WCF引用 代码

    方法1: using (ChannelFactory<ICommonService> channelFactory = new ChannelFactory<ICommonServi ...

  7. LeetCode232:Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...

  8. mysql 建立表之间关系 练习 1

    练习:账号信息表,用户组,主机表,主机组 #用户表 mysql> create table user( id int not null unique auto_increment, userna ...

  9. codeigniter 中使用 phpexcel

    参考:Easily integrate/load PHPExcel into CodeIgniter Framework In order to get PHPExcel working with C ...

  10. PAT 1047 Student List for Course[一般]

    1047 Student List for Course (25 分) Zhejiang University has 40,000 students and provides 2,500 cours ...