博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSON数据格式
阅读量:7193 次
发布时间:2019-06-29

本文共 4733 字,大约阅读时间需要 15 分钟。

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

 

转载于:https://www.cnblogs.com/yslf/p/10846487.html

你可能感兴趣的文章
字符串过滤掉所有最邻近的“<”和“>”之间的字符
查看>>
Matlab中min/max函数的误解
查看>>
ThinkPHP框架视图详细介绍 View 视图--模板(九)
查看>>
BZOJ4123 : [Baltic2015]Hacker
查看>>
蓝牙介绍
查看>>
BZOJ1110 : [POI2007]砝码Odw
查看>>
也谈C#之Json,从Json字符串到类代码
查看>>
javascript事件流机制
查看>>
谈话Java在ThreadLocal理解类
查看>>
随笔2
查看>>
ListView嵌套GridView显示不完整的解决方案
查看>>
创建文件/目录
查看>>
TPS和事务响应时间的关系
查看>>
throw new OAException执行了,却没有正常抛出异常!
查看>>
BZOJ3456 : 城市规划
查看>>
图片懒加载
查看>>
Appium入门示例(Java)
查看>>
Android Studio导入GitHub上的项目常见问题(以图片轮播开源项目为实例)
查看>>
【<td>】使<td>标签内容居上
查看>>
出现The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误
查看>>