zoukankan      html  css  js  c++  java
  • json对象的传输函數的定義

    /**
    * List转换json格式向前端传递json数据流
    * @param list
    * @param response
    * @throws IOException
    */
    public void writeJson(List list,HttpServletResponse response) throws IOException{
    JSONArray jarray=JSONArray.fromObject(list);
    response.setHeader("Content-type", "text/json;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(jarray.toString());
    }
    /**
    *
    * 将一个字段以{key,value}转为json格式字符串返回ajax回调函数
    * @param key
    * @param object
    * @param response
    * @throws IOException
    */
    public void toJson(String key,Object object, HttpServletResponse response) throws IOException{
    JSONObject obj=new JSONObject();
    obj.put(key, object);
    response.setHeader("Content-type", "text/json;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(obj.toString());
    }

    /**
    *
    * 普通类型的bean转换成json格式字符串返回ajax回调函数
    * @param object
    * @param response
    * @throws IOException
    */
    public void toJson(Object object, HttpServletResponse response) throws IOException{
    JSONObject obj=JSONObject.fromObject(object);
    response.setHeader("Content-type", "text/json;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(obj.toString());
    }

    ////////////////////////////////////////
    用法(调用函数,把对象放入函数中): writeJson(items, response);

  • 相关阅读:
    C#学习笔记
    Visual Studio 快捷键
    java 8 中lambda表达式学习
    Spfa算法
    dijkstra算法
    topSort
    并查集--学习详解
    trie树--详解
    POJ1988 并查集的使用
    Mybatis的一级缓存和二级缓存
  • 原文地址:https://www.cnblogs.com/OP-RONG/p/4193344.html
Copyright © 2011-2022 走看看