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。

相关推荐
9523610 分钟前
Spring-cloud配置中心
java·spring·spring cloud·微服务
ydd10010021 分钟前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
真上帝的左手2 小时前
10. 软件设计&架构-Spring Security 7 整合CAS SSO 单点登录
java·spring·架构·sso
Zane19942 小时前
单例线程安全、生产者消费者、死锁:并发面试三连问串讲
java·后端
云烟成雨TD3 小时前
Micrometer 系列【42】链路追踪:Span 体系 | 核心 API
java·链路追踪·micrometer
Gorway3 小时前
理解 Spring 依赖注入:从构造器注入到集合与条件 Bean
java·后端
LiLiYuan.3 小时前
【字符串常量池】
java·开发语言·面试
Sylvia33.3 小时前
从轮询到推送:足球数据API架构演进与火星数据技术拆解
java·服务器·网络·python·websocket·架构
cfm_29143 小时前
高并发系统缓存全解
java·缓存
Ghost Face...3 小时前
龙芯Docker全流程:安装到离线迁移实战
java·docker·eureka