目录
1、JSON概念
2、JSON语法
2.1、基本规则
2.2、获取数据
3、JSON数据和Java对象的相互转换
3.1、JSON转为Java对象(jackson解析器)
3.2、Java对象转为JSON(jackson解析器)
4、案例:校验用户名是否存在
1、JSON概念
JSON(Javascript Object Notation,Javascript对象表示法)。JSON现在多用于存储和交换文本信息的语法,进行数据的传输,JSON比XML更小、更快、更易解析。
2、JSON语法
2.1、基本规则
1、数据在名称/值对中;
JSON数据是由键值对构成的。
键用""引起来(''也可),也可以不使用引号。
值的取值类型:
1)数字(整数或浮点数);
2)字符串(在 "" 中);
3)逻辑值(true或false);
4)数组(在 [] 中);
5)对象(在 {} 中);如 {"address":{"province:'陕西',..."}}
6)null
2、数据由逗号分割;
多个键值对由 , 分隔。
3、花括号保存对象;
使用 {} 定义JSON格式。
4、方括号保存数组。
[]
2.2、获取数据
1、JSON对象.键名
2、JSON对象["键名"]
3、数组对象[索引]
JSON数据语法
[点击并拖拽以移动]
JSON数据语法_遍历
3、JSON数据和Java对象的相互转换
JSON解析器:
*常见的解析器:Jsonlib,Gson,fastjson,jackson
3.1、JSON转为Java对象(jackson解析器)
使用步骤:
1、导入jackson的相关jar包;
2、创建Jackson核心对象 ObjectMapper;
3、调用ObjectMapper的相关方法进行转换。
1)readValue(json字符串数据,Class)
3.2、Java对象转为JSON(jackson解析器)
使用步骤:
1、导入jackson的相关jar包;
2、创建Jackson核心对象 ObjectMapper;
3、调用ObjectMapper的相关方法进行转换。
1)mapper对象转换相关方法
writevalue(参数1,obj):
参数1:
File:将obj对象转为JSON字符串,并保存到指定的文件中。
Writer:将obj对象转为JSON字符串,并将json数据填充到字符输出流中。
OutputStream:将obj对象转为JSON字符串,并将json数据填充到字节输出流中
writevalueAsString(obj):将对象转为json字符串
2)注解
① @JsonIgnore:排除属性
② @JsonFormat:属性值的格式化
3)复杂Java对象转换
List
Map
package com.clp.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Date;
public class Person {
private String name;
private int age;
private String gender;
// @JsonIgnore //忽略该属性
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
", gender='" + gender + ''' +
", birthday=" + birthday +
'}';
}
}
package com.clp.test;
import com.clp.domain.Person;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class JacksonTest {
//Java转为JSON字符串
@Test
public void test1() throws IOException {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
//2、创建Jackson的核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、转换
String json = mapper.writevalueAsString(p);
System.out.println(json); //{"name":"张三","age":23,"gender":"男"}
//writevalue(),将数据写到d://a.txt文件中
// mapper.writevalue(new File("d://a.txt"),p);
//writevalue(),将数据关联到Writer中
// mapper.writevalue(new FileWriter("d://b.txt"),p);
}
@Test
public void test2() throws Exception {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(p);
System.out.println(json);
}
@Test
public void test3() throws JsonProcessingException {
//1、创建Person对象
Person p1 = new Person();
p1.setName("张三");
p1.setAge(23);
p1.setGender("男");
p1.setBirthday(new Date());
Person p2 = new Person();
p2.setName("张三");
p2.setAge(23);
p2.setGender("男");
p2.setBirthday(new Date());
Person p3 = new Person();
p3.setName("张三");
p3.setAge(23);
p3.setGender("男");
p3.setBirthday(new Date());
//创建List集合
List ps = new ArrayList();
ps.add(p1);
ps.add(p2);
ps.add(p3);
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(ps);
// [{},{},{}]
System.out.println(json);
}
@Test
public void test4() throws JsonProcessingException {
//1、创建Person对象
//创建Map集合
Map map = new HashMap();
map.put("name","张三");
map.put("age",23);
map.put("gender","男");
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(map);
// {""="",""=xx,""=""}
System.out.println(json);
}
@Test
public void test5() throws IOException {
//初始化JSON字符串
String json = "{"gender":"男","name":"张三","age":23}";
//创建ObkectMapp对象
ObjectMapper mapper = new ObjectMapper();
//转换为Java对象 Person对象
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
}
}
4、案例:校验用户名是否存在
服务器响应的数据在客户端使用时,要想当作JSON数据格式使用:
1)$.get(type):将最后一个参数指定为 "json"
2)在服务器端设置MIME类型:
response.setContentType("application/json;charset=utf-8");
注册页面
1、数据在名称/值对中;
JSON数据是由键值对构成的。
键用""引起来(''也可),也可以不使用引号。
值的取值类型:
1)数字(整数或浮点数);
2)字符串(在 "" 中);
3)逻辑值(true或false);
4)数组(在 [] 中);
5)对象(在 {} 中);如 {"address":{"province:'陕西',..."}}
6)null
2、数据由逗号分割;
多个键值对由 , 分隔。
3、花括号保存对象;
使用 {} 定义JSON格式。
4、方括号保存数组。
[]
2.2、获取数据
1、JSON对象.键名
2、JSON对象["键名"]
3、数组对象[索引]
JSON数据语法
[点击并拖拽以移动]
JSON数据语法_遍历
3、JSON数据和Java对象的相互转换
JSON解析器:
*常见的解析器:Jsonlib,Gson,fastjson,jackson
3.1、JSON转为Java对象(jackson解析器)
使用步骤:
1、导入jackson的相关jar包;
2、创建Jackson核心对象 ObjectMapper;
3、调用ObjectMapper的相关方法进行转换。
1)readValue(json字符串数据,Class)
3.2、Java对象转为JSON(jackson解析器)
使用步骤:
1、导入jackson的相关jar包;
2、创建Jackson核心对象 ObjectMapper;
3、调用ObjectMapper的相关方法进行转换。
1)mapper对象转换相关方法
writevalue(参数1,obj):
参数1:
File:将obj对象转为JSON字符串,并保存到指定的文件中。
Writer:将obj对象转为JSON字符串,并将json数据填充到字符输出流中。
OutputStream:将obj对象转为JSON字符串,并将json数据填充到字节输出流中
writevalueAsString(obj):将对象转为json字符串
2)注解
① @JsonIgnore:排除属性
② @JsonFormat:属性值的格式化
3)复杂Java对象转换
List
Map
package com.clp.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Date;
public class Person {
private String name;
private int age;
private String gender;
// @JsonIgnore //忽略该属性
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
", gender='" + gender + ''' +
", birthday=" + birthday +
'}';
}
}
package com.clp.test;
import com.clp.domain.Person;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class JacksonTest {
//Java转为JSON字符串
@Test
public void test1() throws IOException {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
//2、创建Jackson的核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、转换
String json = mapper.writevalueAsString(p);
System.out.println(json); //{"name":"张三","age":23,"gender":"男"}
//writevalue(),将数据写到d://a.txt文件中
// mapper.writevalue(new File("d://a.txt"),p);
//writevalue(),将数据关联到Writer中
// mapper.writevalue(new FileWriter("d://b.txt"),p);
}
@Test
public void test2() throws Exception {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(p);
System.out.println(json);
}
@Test
public void test3() throws JsonProcessingException {
//1、创建Person对象
Person p1 = new Person();
p1.setName("张三");
p1.setAge(23);
p1.setGender("男");
p1.setBirthday(new Date());
Person p2 = new Person();
p2.setName("张三");
p2.setAge(23);
p2.setGender("男");
p2.setBirthday(new Date());
Person p3 = new Person();
p3.setName("张三");
p3.setAge(23);
p3.setGender("男");
p3.setBirthday(new Date());
//创建List集合
List ps = new ArrayList();
ps.add(p1);
ps.add(p2);
ps.add(p3);
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(ps);
// [{},{},{}]
System.out.println(json);
}
@Test
public void test4() throws JsonProcessingException {
//1、创建Person对象
//创建Map集合
Map map = new HashMap();
map.put("name","张三");
map.put("age",23);
map.put("gender","男");
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(map);
// {""="",""=xx,""=""}
System.out.println(json);
}
@Test
public void test5() throws IOException {
//初始化JSON字符串
String json = "{"gender":"男","name":"张三","age":23}";
//创建ObkectMapp对象
ObjectMapper mapper = new ObjectMapper();
//转换为Java对象 Person对象
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
}
}
4、案例:校验用户名是否存在
服务器响应的数据在客户端使用时,要想当作JSON数据格式使用:
1)$.get(type):将最后一个参数指定为 "json"
2)在服务器端设置MIME类型:
response.setContentType("application/json;charset=utf-8");
注册页面
JSON解析器:
*常见的解析器:Jsonlib,Gson,fastjson,jackson
3.1、JSON转为Java对象(jackson解析器)
使用步骤:
1、导入jackson的相关jar包;
2、创建Jackson核心对象 ObjectMapper;
3、调用ObjectMapper的相关方法进行转换。
1)readValue(json字符串数据,Class)
3.2、Java对象转为JSON(jackson解析器)
使用步骤:
1、导入jackson的相关jar包;
2、创建Jackson核心对象 ObjectMapper;
3、调用ObjectMapper的相关方法进行转换。
1)mapper对象转换相关方法
writevalue(参数1,obj):
参数1:
File:将obj对象转为JSON字符串,并保存到指定的文件中。
Writer:将obj对象转为JSON字符串,并将json数据填充到字符输出流中。
OutputStream:将obj对象转为JSON字符串,并将json数据填充到字节输出流中
writevalueAsString(obj):将对象转为json字符串
2)注解
① @JsonIgnore:排除属性
② @JsonFormat:属性值的格式化
3)复杂Java对象转换
List
Map
package com.clp.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Date;
public class Person {
private String name;
private int age;
private String gender;
// @JsonIgnore //忽略该属性
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
", gender='" + gender + ''' +
", birthday=" + birthday +
'}';
}
}
package com.clp.test;
import com.clp.domain.Person;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class JacksonTest {
//Java转为JSON字符串
@Test
public void test1() throws IOException {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
//2、创建Jackson的核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、转换
String json = mapper.writevalueAsString(p);
System.out.println(json); //{"name":"张三","age":23,"gender":"男"}
//writevalue(),将数据写到d://a.txt文件中
// mapper.writevalue(new File("d://a.txt"),p);
//writevalue(),将数据关联到Writer中
// mapper.writevalue(new FileWriter("d://b.txt"),p);
}
@Test
public void test2() throws Exception {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(p);
System.out.println(json);
}
@Test
public void test3() throws JsonProcessingException {
//1、创建Person对象
Person p1 = new Person();
p1.setName("张三");
p1.setAge(23);
p1.setGender("男");
p1.setBirthday(new Date());
Person p2 = new Person();
p2.setName("张三");
p2.setAge(23);
p2.setGender("男");
p2.setBirthday(new Date());
Person p3 = new Person();
p3.setName("张三");
p3.setAge(23);
p3.setGender("男");
p3.setBirthday(new Date());
//创建List集合
List ps = new ArrayList();
ps.add(p1);
ps.add(p2);
ps.add(p3);
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(ps);
// [{},{},{}]
System.out.println(json);
}
@Test
public void test4() throws JsonProcessingException {
//1、创建Person对象
//创建Map集合
Map map = new HashMap();
map.put("name","张三");
map.put("age",23);
map.put("gender","男");
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(map);
// {""="",""=xx,""=""}
System.out.println(json);
}
@Test
public void test5() throws IOException {
//初始化JSON字符串
String json = "{"gender":"男","name":"张三","age":23}";
//创建ObkectMapp对象
ObjectMapper mapper = new ObjectMapper();
//转换为Java对象 Person对象
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
}
}
4、案例:校验用户名是否存在
服务器响应的数据在客户端使用时,要想当作JSON数据格式使用:
1)$.get(type):将最后一个参数指定为 "json"
2)在服务器端设置MIME类型:
response.setContentType("application/json;charset=utf-8");
注册页面
使用步骤:
1、导入jackson的相关jar包;
2、创建Jackson核心对象 ObjectMapper;
3、调用ObjectMapper的相关方法进行转换。
1)mapper对象转换相关方法
writevalue(参数1,obj):
参数1:
File:将obj对象转为JSON字符串,并保存到指定的文件中。
Writer:将obj对象转为JSON字符串,并将json数据填充到字符输出流中。
OutputStream:将obj对象转为JSON字符串,并将json数据填充到字节输出流中
writevalueAsString(obj):将对象转为json字符串
2)注解
① @JsonIgnore:排除属性
② @JsonFormat:属性值的格式化
3)复杂Java对象转换
List
Map
package com.clp.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Date;
public class Person {
private String name;
private int age;
private String gender;
// @JsonIgnore //忽略该属性
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
", gender='" + gender + ''' +
", birthday=" + birthday +
'}';
}
}
package com.clp.test;
import com.clp.domain.Person;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class JacksonTest {
//Java转为JSON字符串
@Test
public void test1() throws IOException {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
//2、创建Jackson的核心对象 ObjectMapper
ObjectMapper mapper = new ObjectMapper();
//3、转换
String json = mapper.writevalueAsString(p);
System.out.println(json); //{"name":"张三","age":23,"gender":"男"}
//writevalue(),将数据写到d://a.txt文件中
// mapper.writevalue(new File("d://a.txt"),p);
//writevalue(),将数据关联到Writer中
// mapper.writevalue(new FileWriter("d://b.txt"),p);
}
@Test
public void test2() throws Exception {
//1、创建Person对象
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
p.setBirthday(new Date());
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(p);
System.out.println(json);
}
@Test
public void test3() throws JsonProcessingException {
//1、创建Person对象
Person p1 = new Person();
p1.setName("张三");
p1.setAge(23);
p1.setGender("男");
p1.setBirthday(new Date());
Person p2 = new Person();
p2.setName("张三");
p2.setAge(23);
p2.setGender("男");
p2.setBirthday(new Date());
Person p3 = new Person();
p3.setName("张三");
p3.setAge(23);
p3.setGender("男");
p3.setBirthday(new Date());
//创建List集合
List ps = new ArrayList();
ps.add(p1);
ps.add(p2);
ps.add(p3);
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(ps);
// [{},{},{}]
System.out.println(json);
}
@Test
public void test4() throws JsonProcessingException {
//1、创建Person对象
//创建Map集合
Map map = new HashMap();
map.put("name","张三");
map.put("age",23);
map.put("gender","男");
//2、转换
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writevalueAsString(map);
// {""="",""=xx,""=""}
System.out.println(json);
}
@Test
public void test5() throws IOException {
//初始化JSON字符串
String json = "{"gender":"男","name":"张三","age":23}";
//创建ObkectMapp对象
ObjectMapper mapper = new ObjectMapper();
//转换为Java对象 Person对象
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
}
}
4、案例:校验用户名是否存在
服务器响应的数据在客户端使用时,要想当作JSON数据格式使用:
1)$.get(type):将最后一个参数指定为 "json"
2)在服务器端设置MIME类型:
response.setContentType("application/json;charset=utf-8");
注册页面
package com.clp.web.servlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取用户名
String username = request.getParameter("username");
//2、调用service层判断用户名是否存在
//设置响应的数据格式为JSON:(这样设置就不需要再在$.get(type)设置类型为json了)
// response.setContentType("application/json;charset=utf-8");
response.setContentType("text/html;charset=utf-8"); //解决乱码
Map map = new HashMap();
if("tom".equals(username)) {
//存在
map.put("userExist",true);
map.put("msg","此用户名太受欢迎,请更换一个");
}else {
//不存在
map.put("userExist",false);
map.put("msg","用户名可用");
}
//将map转为json,并且传递给客户端浏览器
ObjectMapper mapper = new ObjectMapper();
mapper.writevalue(response.getWriter(),map);
}
}



