JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script type="text/javascript">
var user= {
name:"xxx",
age:3,
sex:"男"
}
//将js对象转换为json字符串
var json = JSON.stringify(user)
//将json字符串转换为js对象
var user2= JSON.parse(json)
console.log(json)
console.log("=======================")
console.log(user2)
script>
head>
<body>
body>
html>
1.导入Jackson依赖
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.13.4.2version>
dependency>
2.配置json乱码问题
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
bean>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
3.编写User类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
public String name;
public int age;
public String sex;
}
4.编写UserController类
@RestController //和下面的@ResponseBody一个效果
//@Controller
public class UserController {
//@RequestMapping中有一个produces = "application/json;charset=utf-8" 处理json乱码问题
@RequestMapping(value = "/j1")
//@ResponseBody//不走视图解析器,只返回字符串
public String json1() throws JsonProcessingException {
//jackson中有一个对象叫ObjectMapper
ObjectMapper mapper = new ObjectMapper();
User user = new User("xxx",24,"男");
String s = mapper.writeValueAsString(user);
return s;
}
@RequestMapping(value = "/j2")
public String json2() throws JsonProcessingException {
//jackson中有一个对象叫ObjectMapper
ObjectMapper mapper = new ObjectMapper();
List<User> list = new ArrayList<>();
User user1 = new User("xxx",24,"男");
User user2 = new User("xxx",24,"男");
User user3 = new User("xxx",24,"男");
User user4 = new User("xxx",24,"男");
User user5 = new User("xxx",24,"男");
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
list.add(user5);
String s = mapper.writeValueAsString(list);
return s;
}
返回json字符串形式
在页面以json字符串输出时间(两种方式)
@RequestMapping(value = "/j3")
public String json3() throws JsonProcessingException {
//jackson中有一个对象叫ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//ObjectMapper,时间解析后的默认格式为时间戳Timestamp 1970.1.1开始
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return mapper.writeValueAsString(sdf.format(date));
}
@RequestMapping(value = "/j4")
public String json4() throws JsonProcessingException {
//jackson中有一个对象叫ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//关闭默认的时间戳格式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(sdf);
Date date = new Date();
return mapper.writeValueAsString(date);
}
提取重复的操作制作工具类
public class JsonUtils {
//方法重载,直接使用已经写好的方法,第二个参数可以提供一下空字符串返回
public static String getJson(Object object) throws JsonProcessingException {
return getJson(object,"");
}
public static String getJson(Object object,String dateFormat) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
mapper.setDateFormat(sdf);
return mapper.writeValueAsString(object);
}
}
/j4代码就可以精简成下述/j5
@RequestMapping(value = "/j5")
public String json5() throws JsonProcessingException {
Date date = new Date();
return JsonUtils.getJson(date,"yyyy-MM-dd HH:mm:ss");
}
/j1代码就可以精简成下述
@RequestMapping(value = "/j1")
public String json1() throws JsonProcessingException {
return JsonUtils.getJson(new User("xxx",24,"男"));
}
1.导入Fastjson依赖
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.16version>
dependency>
2.使用Fastjson
@RequestMapping(value = "/j6")
public String json6() throws JsonProcessingException {
List<User> list = new ArrayList<>();
User user1 = new User("姚秉文",24,"男");
User user2 = new User("姚秉文",24,"男");
User user3 = new User("姚秉文",24,"男");
User user4 = new User("姚秉文",24,"男");
User user5 = new User("姚秉文",24,"男");
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
list.add(user5);
return JSON.toJSONString(list);
}
使用JSON.方法直接进行使用