重庆技术网站建设做视频点播网站
web/
2025/10/6 12:20:39/
文章来源:
重庆技术网站建设,做视频点播网站,wordpress 模板 分栏目,上饶营销网站建设json比较可读#xff0c;通过键值对返回。实现通常有两种方式:一种是自己来构造#xff0c;也就是用一个对象存储数据#xff0c;在最后输出时将其json字符串化#xff1b;第二种是使用 RestController 注解实现json数据返回。 第一种
导入依赖坐标#xff1a; depe… json比较可读通过键值对返回。实现通常有两种方式:一种是自己来构造也就是用一个对象存储数据在最后输出时将其json字符串化第二种是使用 RestController 注解实现json数据返回。 第一种
导入依赖坐标 dependency
!-- json--groupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-json/artifactId/dependency
实体类
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进行测试
RestController
RequestMapping(/test_json)
public class TestJson {Autowiredpublic Testjson testjson;RequestMapping(/pet)public Pet getPet(){return new Pet(1L,团团,2,black,a cut panda);}RequestMapping(/petList)public ListPet getPetList(){ListPet 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。依赖配置 groupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion2.0.25/version 测试提供的实体类
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;}
}测试类
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);ListUser users new ArrayList();users.add(user1);users.add(user2);String ListUserJson JSON.toJSONString(users);System.out.println(ListObject 转 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。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/87907.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!