zoukankan      html  css  js  c++  java
  • JSON数据格式

    JSON数据格式
      JSON 是一种数据传输格式
      JSON 是要结合 Ajax(异步请求) 使用的,
      在后端一般会将一个对象转换成 JSON 格式的数据之后返回给客户端,
      可以自己写工具转换, 也可以使用第三方工具 : gjson, fastjson 等

    Demo: JSON 表示一个数字
    2.90

    Demo: JSON 表示一个字符串
    "Hello world"

    Demo: JSON 表示一个对象
    {
      "name":"smith".
      "age":30,
      "sex":"男"
    }

    Demo: JSON对象的属性可以是对象
    {
      "name":"smith".
      "age":28,
      "sex":"男"
      "school":{
      "sname":"南京大学".
      "address":"南京市鼓楼区汉口路22号"
    }
    }

    Demo: JSON 格式表示数组
    保存名字的数组: ["张三","李四","王五"]
    保存雇员的信息: ["smith",1001,"clerck",7788,2000.00,200.0]
    [
      ["smith",1001,"clerck",7788,2000.00,200.0]
      ["smith",1001,"clerck",7788,2000.00,200.0]
      ["smith",1001,"clerck",7788,2000.00,200.0]
    ]
    [
      {"name":"smith","empno":1001,"job":"clerck","sal":9000.00,"comm":5000.00},
      {"name":"smith","empno":1001,"job":"clerck","sal":9000.00,"comm":5000.00},
      {"name":"smith","empno":1001,"job":"clerck","sal":9000.00,"comm":5000.00},
    ]

    Demo: 对象数组
    在一个数组保存多个 json 对象 (在一个数组中保存多个对象)
    [
      {
        "title":"Java 开发",
        "edition":3,
        "author":["smith","张三","李四"]
      },
      {
        "title":"Web 开发",
        "edition":3,
        "author":["Allen","王五","赵六"]
      }
    ]
    二维数组保存
    [
      ["Java 开发",3,["smith","张三","李四"]],
      ["Web 开发",3["Allen","王五","赵六"]]
    ]

    Demo: 将一个对象转换成 json 数据

    @WebServlet(urlPatterns= {"/emp/*"})
    public class EmpServlet extends BaseServlte{
        private IEmpService empservice = (IEmpService)ServiceFactory.getInstance(EmpServiceImpl.class);
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String pathInfo = req.getPathInfo();
                try {
                    if ("/getOne".equals(pathInfo))) {
                        this.getOne(req, resp);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        //将一个对象转换成 json 数据输出到客户端
        public void getOne(HttpServletRequest req, HttpServletResponse resp) throws Exception {
            //获取要查询的雇员编号
            Integer id = Integer.parseInt(req.getParameter("id"));
            Emp emp = empservice.findEmpById(id);
            //将查询到的对象转换成json 数据格式
            String jsonEmp = JSON.toJSONString(emp);
            System.out.println(jsonEmp);
            PrintWriter out = null;
            out=resp.getWriter();
            //将转换后的 json 数据输出到客户端
            out.print(jsonEmp);
            out.close();
        }
    }

    Demo: 将一个 List 集合转换为 json 数据

    @WebServlet(urlPatterns= {"/emp/*"})
    public class EmpServlet extends BaseServlte{
        private IEmpService empservice = (IEmpService)ServiceFactory.getInstance(EmpServiceImpl.class);
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String pathInfo = req.getPathInfo();
                try {
                    if ("/getOne".equals(pathInfo))) {
                        this.getOne(req, resp);
                    }else if ("/jsonList".equals(pathInfo)) {
                        this.jsonList(req, resp);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        //模糊分页查询: 将一个 List 集合转换为 json 数据
        public void jsonList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
            String kw = req.getParameter("kw");
            Integer cp = Integer.parseInt(req.getParameter("cp"));
            Integer ls = Integer.parseInt(req.getParameter("ls"));
            //将 List 集合转换为 json 数据格式
            String jsonListEmp = JSON.toJSONString(this.empservice.findAllSplit(kw, cp, ls).get("emplist"));
            System.out.println(jsonListEmp);
        }
    }

    Demo: 将Map 数据转换为 json 数据
      转换 Mao 集合则是键值对的形式

    @WebServlet(urlPatterns= {"/emp/*"})
    public class EmpServlet extends BaseServlte{
        private IEmpService empservice = (IEmpService)ServiceFactory.getInstance(EmpServiceImpl.class);
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String pathInfo = req.getPathInfo();
                try {
                    if ("/getOne".equals(pathInfo))) {
                        this.getOne(req, resp);
                    }else if ("/jsonList".equals(pathInfo)) {
                        this.jsonList(req, resp);
                    } else if ("/jsonMap".equals(pathInfo)) {
                        this.jsonMap(req, resp);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        
        //模糊分页查询: 将一个 Map 集合转换为 json 数据
        public void jsonMap(HttpServletRequest req, HttpServletResponse resp) throws Exception {
            String kw = req.getParameter("kw");
            Integer cp = Integer.parseInt(req.getParameter("cp"));
            Integer ls = Integer.parseInt(req.getParameter("ls"));
            //将 Map 集合转换为 json 数据格式
            String jsonMapEmp = JSON.toJSONString(this.empservice.findAllSplit(kw, cp, ls));
            System.out.println(jsonMapEmp);
        }
    }
  • 相关阅读:
    【转】sublime text 2 中文乱码解决办法
    vi使用入门指南
    【原创】基于部署映像服务和管理(DISM)修改映象解决WIN7 USB3.0安装时报错
    日语五十音图快速记忆法
    深度阅读:C语言指针,从底层原理到花式技巧,图文+代码透析
    全民热衷“合成大西瓜”,游戏外挂上热搜,不愧是程序员!
    零基础想要转行成为程序员?这几点你要知道
    恩怨纠缠的苹果和微信!苹果底层开源代码中包含兼容微信的代码,这是苹果偷学代码啦?
    好你个C语言,原来还有这么多副面孔!
    “熊孩子”乱敲键盘就攻破了Linux桌面,其父亲发现linux漏洞
  • 原文地址:https://www.cnblogs.com/yslf/p/10846487.html
Copyright © 2011-2022 走看看