什么是JSON?
- JSON 英文全称 JavaScript Object Notation
- JSON 是一种轻量级的数据交换格式,用于存储和传输数据的格式,通常用于服务端向网页传递数据。
- JSON是独立的语言
- JSON 易于理解。
注意: JSON 使用 JavaScript 语法,但是 JSON 格式仅仅是一个文本。 文本可以被任何编程语言读取及作为数据格式传递。
1.JSON语法规则
- 数据为键/值对的形式。
- 数据由逗号分隔。
- 大括号保存对象
- 方括号保存数组
json对象示例
//json对象
var person={
"name":"张三",
"age":"18",
"hobby":["篮球","学Java","研究算法"],
"location":{
"province":"陕西省",
"city":"西安市"
}
};
alert(person.name)//可以直接调用
json字符串示例
var str=`{"name":"张三","age":24}`;
//需要转换为JSON对象之后才能通过键来访问
2.js中使用JSON
JSON字符串转换成JSON对象
JSON.parse()
通常都是后台把数据以字符串的形式来封装,然后响应给前台,前台就需要将JSON字符串转换成JSON对象来解析。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var str=`{"name":"张三","age":24}`;//注意:需要用` `将数据符号包裹起来
//将json字符串转为json对象
var jsonstr=JSON.parse(str)
alert(jsonstr.name)
alert(jsonstr.age)
</script>
</html>
JSON对象转换为字符串
JSON.stringify()
案例演示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var obj={
"username":"zahngsan",
"password":"123456",
"sex":"nan",
"age":19
}
var str=JSON.stringify(obj)
alert(typeof str)
alert(str)
</script>
</html>
3.Java中JSON字符串和Java对象的互相转换
Java中将JSON字符串转换为Java对象通常采用的是解析器,常见的解析器
Jsonlib,Gson,fastjson,jackson
(1)Java对象转换JSON
- 导入jackson相关的jar包
- 创建jackson核心对象 objectMapper
- 调用objectMapper的相关方法进行转换
转换方法
1.writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
2.writeValueAsString(obj):将对象转为json字符串
注解的使用
@JsonIgnore:排除某个属性不要转换成JSON,给类的属性上加上这个注解。
@JsonFormat:属性值得格式化日期字符串,取的是默认时区的时间
案例演示
package com.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class Test {
public static void main(String[] args) throws IOException {
//1.导入jackson的jar包
//2.依赖jar包
Student student1 = new Student("张三", "123456", 16,new Date());
//2.创建对象使用
ObjectMapper objectMapper = new ObjectMapper();
//3.调用转换方法
String jsonstr = objectMapper.writeValueAsString(student1);
System.out.println(jsonstr);//{"username":"张三","password":"123456","age":16}
//writeValue方法中还可以对将转换后的json对象进行io操作
objectMapper.writeValue(new File("Student.json"),student1);//保存到指定文件中
}
}
class Student {
private String username;
private String password;
//让年龄不能被转换为JSON
@JsonIgnore
private int age;
//格式化日期类型
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
private Date birthday;
public Student(){
}
public Student(String username,String password,int age,Date birthday){
this.username=username;
this.password=password;
this.age=age;
this.birthday=birthday;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
(2)JSON转为Java对象
转换方法
1.导入jackson的相关jar包
2. 创建Jackson核心对象 ObjectMapper
3. 调用ObjectMapper的相关方法进行转换
readValue(json字符串数据,Class)
案例演示
package com.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
//JSON对象转换为Java对象
//{"username":"张三","password":"123456","age","18"}//直接复制过来 自动回加上转译字符
String json="{\"username\":\"张三\",\"password\":\"123456\",\"age\":\"18\"}";
ObjectMapper objectMapper = new ObjectMapper();
Teacher teacher = objectMapper.readValue(json, Teacher.class);
System.out.println(teacher);//Teacher{username='张三', password='123456', age=18}
}
}
class Teacher {
private String username;
private String password;
private int age;
public Teacher(){
}
public Teacher(String username,String password,int age){
this.username=username;
this.password=password;
this.age=age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}