导入fastJson包
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>fastjson</artifactId> |
| <version>1.2.62</version> |
| </dependency> |
编写Conroller
| |
| |
| package com.Google.controller; |
| |
| import com.Google.pojo.User; |
| import com.alibaba.fastjson.JSON; |
| import com.alibaba.fastjson.JSONObject; |
| import com.fasterxml.jackson.core.JsonProcessingException; |
| import com.fasterxml.jackson.databind.ObjectMapper; |
| import com.fasterxml.jackson.databind.SerializationFeature; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.ResponseBody; |
| import org.springframework.web.bind.annotation.RestController; |
| |
| import java.text.SimpleDateFormat; |
| import java.util.ArrayList; |
| import java.util.Date; |
| import java.util.List; |
| |
| |
| @RestController |
| public class jsonController { |
| @RequestMapping("/j1") |
| |
| public String json1() throws JsonProcessingException { |
| |
| ObjectMapper mapper = new ObjectMapper(); |
| User user = new User("Spring", 2, "女"); |
| String str = mapper.writeValueAsString(user); |
| return str; |
| } |
| |
| @RequestMapping("/j2") |
| public String json2() throws JsonProcessingException { |
| ObjectMapper mapper = new ObjectMapper(); |
| List<User> users = new ArrayList<>(); |
| User user = new User("Spring", 2, "女"); |
| User user1 = new User("Spring", 2, "女"); |
| User user2 = new User("Spring", 2, "女"); |
| User user3 = new User("Spring", 2, "女"); |
| users.add(user); |
| users.add(user1); |
| users.add(user2); |
| users.add(user3); |
| return mapper.writeValueAsString(users); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @RequestMapping("/j3") |
| public String json3() throws JsonProcessingException { |
| ObjectMapper mapper = new ObjectMapper(); |
| |
| mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
| Date date = new Date(); |
| |
| SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); |
| mapper.setDateFormat(dateFormat); |
| return mapper.writeValueAsString(date); |
| } |
| @RequestMapping("/j4") |
| public String json4() throws JsonProcessingException { |
| List<User> users = new ArrayList<>(); |
| User user = new User("Spring", 2, "女"); |
| User user1 = new User("Spring1", 21, "女"); |
| User user2 = new User("Spring2", 22, "女"); |
| User user3 = new User("Spring3", 23, "女"); |
| users.add(user); |
| users.add(user1); |
| users.add(user2); |
| users.add(user3); |
| |
| |
| String jsonString = JSON.toJSONString(users); |
| System.out.println("java对象转换成JSON字符串:"+jsonString); |
| ArrayList jsonObject = JSON.parseObject(jsonString, ArrayList.class); |
| System.out.println("JSON字符串转换成java对象:"+jsonObject); |
| |
| JSONObject json = (JSONObject) JSON.toJSON(user); |
| System.out.println("JSON对象name属性的值:"+json.getString("name")); |
| |
| User user4 = JSON.toJavaObject(json, User.class); |
| System.out.println("java对象的name属性值:"+user4.getSex()); |
| |
| |
| return "ha"; |
| } |
| } |
java对象转换成JSON字符串
| String jsonString = JSON.toJSONString(users); |
JSON字符串转换成java对象
| ArrayList jsonObject = JSON.parseObject(jsonString, ArrayList.class); |
将java对象转JSON对象
| JSONObject json = (JSONObject) JSON.toJSON(user); |
将JSON对象转java对象
| User user4 = JSON.toJavaObject(json, User.class); |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术