zoukankan      html  css  js  c++  java
  • JSON返回数据的几种方式

    Java开发难免要使用JSON传递数据,下面是我总结的几种回调方式。

    推荐文章:JSON、JSONObject、JSONArray、Map之间的关系

    方式一、返回Map集合

    //返回Map集合
    @RequestMapping("/findStudent")
    @ResponseBody
    public Map<String, Object> findStudent(MultipartFile file) {
    
        Student student = new Student();
        student.setUsername("木心");
        student.setPassword("123456");
    
        Map map = new HashMap<String, Object>();
        map.put("code", 1);
        map.put("msg", "成功");
        map.put("student",student);
    
        return map;
    }

    方式二、返回JSON对象

    //返回JSONObject对象
    @RequestMapping(value = "/findStudent2")
    @ResponseBody
    public JSONObject findStudent2(String id){
    
        Student student = new Student();
        student.setUsername("木心");
        student.setPassword("123456");
    
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 1);
        jsonObject.put("msg", "成功");
        jsonObject.put("student",student);
    
        return jsonObject;
    }

    方式三、返回JSON数组

    //返回JSON数组
    @RequestMapping(value = "/findStudent3")
    @ResponseBody
    public JSONArray findStudent3(String id){
    
        Student student = new Student();
        student.setUsername("木心");
        student.setPassword("123456");
    
        Student student2 = new Student();
        student2.setUsername("小白菜");
        student2.setPassword("1234567");
    
        JSONArray json = new JSONArray();
        json.add(student);
        json.add(student2);
        return  json;
    }
  • 相关阅读:
    15-01-18 C# 面向对象 13
    15-01-15 C# 面向对象 12
    15-01-12 C# 面向对象 11
    15-01-11 C# 面向对象 10
    15-01-10 C# 面向对象 09
    了解 Azure 中的无服务器计算
    了解 Azure 虚拟机
    什么是 Azure?
    云服务的类型
    云部署模型
  • 原文地址:https://www.cnblogs.com/mxxbc/p/14039005.html
Copyright © 2011-2022 走看看