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);

  • 相关阅读:
    从无到有构建vue实战项目(二)
    从无到有构建vue实战项目(一)
    windows下nginx的安装和使用
    系统全局环境变量设置
    100个常用的linux命令
    Linux常用系统信息查看命令
    linux下搜索find命令拾遗
    linux基础优化
    linux系统基础文件属性
    正则awk和查看文件行数
  • 原文地址:https://www.cnblogs.com/OP-RONG/p/4193344.html
Copyright © 2011-2022 走看看