蘇阿細 - fastjson
type: channel
link: https://www.wangchouchou.com/tag/fastjson/
items: JSON

JSON
type: item
date: 2020-12-17T09:00:00+00:00
description: JSONJSON(JavaScript Object Notation，JS标记对象)是一种轻量级的数据交换格式。1、jackson导入依赖 com.fasterxml.jackson.core jackson-databind 2.12.0 Utils工具类package com.sw.utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import java.text.SimpleDateFormat; /** * @Author suaxi * @Date 2020/12/17 16:08 */ public class JsonUtils { public static String getJson(Object object){ return getJson(object,"yyyy-MM-dd HH:mm:ss"); } public static String getJson(Object object,String dateFormate){ ObjectMapper mapper = new ObjectMapper(); //不使用时间戳 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); //自定义时间格式 SimpleDateFormat sdf = new SimpleDateFormat(dateFormate); mapper.setDateFormat(sdf); try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } } Controllerpackage com.sw.controller; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.sw.pojo.User; import com.sw.utils.JsonUtils; 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.util.ArrayList; import java.util.Date; import java.util.List; /** * @Author suaxi * @Date 2020/12/17 15:38 */ //@Controller @RestController public class UserController { @RequestMapping("/j1") //@ResponseBody //不会走视图解析器，直接返回一个字符串 public String test01() throws JsonProcessingException { //jackson ObjectMapper ObjectMapper mapper = new ObjectMapper(); User user = new User("孙笑川", 33, "男"); String s = mapper.writeValueAsString(user); return s; } @RequestMapping("/j2") public String test02() throws JsonProcessingException { List<User> userList = new ArrayList<User>(); User user1 =…