Json数据格式

json比较可读,通过键值对返回。实现通常有两种方式:一种是自己来构造,也就是用一个对象存储数据,在最后输出时将其json字符串化;第二种是使用 @RestController 注解实现json数据返回。

第一种

导入依赖坐标:

XML 复制代码
        <dependency>
<!--            json-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>

实体类:

java 复制代码
public class Pet {
    private Long id;
    private String name;
    private int age;
    private String color;
    private String description;

    public Pet(Long id, String name, int age, String color, String description) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.color = color;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

创建一个Controller进行测试:

java 复制代码
@RestController
@RequestMapping("/test_json")
public class TestJson {
    @Autowired
    public Testjson testjson;
    @RequestMapping("/pet")
    public Pet getPet(){
        return new Pet(1L,"团团",2,"black","a cut panda");
    }
    @RequestMapping("/petList")
    public List<Pet> getPetList(){
        List<Pet> pets = new ArrayList<>();
        pets.add(new Pet(2L,"小狗",3,"write","a dog"));
        pets.add(new Pet(1L,"小猪",2,"pink","a pig"));
        return pets;
    }

}

完成。

第二种

这里使用第三方代替实现,此方法比较粗糙不推荐使用,例如选择 alibaba 开源的 faskjson。依赖配置:

XML 复制代码
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.25</version>

测试提供的实体类:

java 复制代码
package org.example.pojo;

public class User {
    private String name;
    private String password;

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

测试类:

java 复制代码
package org.example.service;

import com.alibaba.fastjson.JSON;
import org.example.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
public class Testjson {
    public String objectToJson(){
        //单个Java 对象
        User user = new User("tfboys","2333");
        String userJsonStr = JSON.toJSONString(user);
        System.out.println("java类转json字符串为:"+userJsonStr);
        //多个java 对象
        User user1 = new User("gameboy","2334");
        User user2 = new User("steatboy","456789");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        String ListUserJson = JSON.toJSONString(users);
        System.out.println("List<Object> 转 json字符串是:"+ListUserJson);
        jsonToObject();
        return ListUserJson.toString();
    }
    public void jsonToObject(){
        String jsonStr1 = "{'password':'123456','name':'dmeget'}";
        User user = JSON.parseObject(jsonStr1, User.class);
        System.out.println("json字符串转简单java对象:"+user.toString());
    }
}

这里使用了两个比较重要的方法:一个是把对象json化的toJsonString方法,另一个是把json对象化的parseObject。

相关推荐
daidaidaiyu2 小时前
一文学习 工作流开发 BPMN、 Flowable
java
ZTLJQ2 小时前
序列化的艺术:Python JSON处理完全解析
开发语言·python·json
SuniaWang3 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
韩立学长3 小时前
Springboot校园跑腿业务系统0b7amk02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
sheji34163 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
m0_726965983 小时前
面面面,面面(1)
java·开发语言
代码栈上的思考4 小时前
消息队列:内存与磁盘数据中心设计与实现
后端·spring
xuhaoyu_cpp_java4 小时前
过滤器与监听器学习
java·经验分享·笔记·学习
程序员小假5 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha5 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端