FastJson用法
1.导入依赖
<dependency>
<groupId>com.jwebmp.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>0.66.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
2. java对象转json
Person person = new Person();
person.setAge(13);
person.setEmail("129duckflew@gmamil.com");
person.setName("张三");
ObjectMapper objectMapper=new ObjectMapper();
/**
* 转换方法
* writeValue(arg1,obj)
* 参数1:
* File :将obj转换为json字符串 并白村到指定的文件
* Writer :将obj对象转换到json 字符串 并且将json数据填充到输出流中
* OutputStream 将obj对象转换到json 字符串 并且将json数据填充到字节输出流中
*/
String json = objectMapper.writeValueAsString(person);
objectMapper.writeValue(new File("F://personJson.txt"),person);
objectMapper.writeValue(new FileWriter(new File("F://personJson.txt")),person);
System.out.println(json);
输出如下
{"email":"129duckflew@gmamil.com","name":"张三","age":13}
F盘下生成了personJson.txt 里面的内容就是上面那句
注解的使用
@JsonIgnore
忽略某个属性 不转化成json
public class Person {
String email;
String name;
int age;
@JsonIgnore
Date birthday;
}
@JsonFormat(pattern = "yyyy-MM-dd")
格式化某个属性
格式化后的json如下
{"email":"129duckflew@gmamil.com","name":"张三","age":13,"birthday":"2020-09-19"}
复杂类型的转换
Person person = new Person();
person.setAge(13);
person.setEmail("129duckflew@gmamil.com");
person.setName("张三");
person.setBirthday(new Date());
Person person1 = new Person();
person1.setAge(13);
person1.setEmail("129duckflew@gmamil.com");
person1.setName("李四");
person1.setBirthday(new Date());
Person person2n = new Person();
person2n.setAge(13);
person2n.setEmail("129dflew@ssssgmamil.com");
person2n.setName("王五");
person2n.setBirthday(new Date());
List<Person> personList=new ArrayList<>();
personList.add(person);
personList.add(person1);
personList.add(person2n);
/**
* 转换
*/
ObjectMapper objectMapper=new ObjectMapper();
String json = objectMapper.writeValueAsString(personList);
System.out.println(json);
输出结果如下
[{"email":"129duckflew@gmamil.com","name":"张三","age":13,"birthday":"2020-09-19"},{"email":"129duckflew@gmamil.com","name":"李四","age":13,"birthday":"2020-09-19"},{"email":"129dflew@ssssgmamil.com","name":"王五","age":13,"birthday":"2020-09-19"}]
另外 如果要这样转换 则 detabind必须为下面这个版本 可能会爆如下错误
NoSuchMethodError:com.fasterxml.jackson.core.JsonGenerator.writeStartArray
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
如果仍然无法解决的话 可能是springboot内置的fastJosn冲突了 那就需要在spring boot web starter中排除掉这个部分 依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
案例--验证注册的用户名是否可用
前端代码--在输入框失去焦点的时候发送ajax请求
还是要先引入Jquery 老生常谈
$(function () {
$("#username").blur(function () {
let username = $(this).val();
$.get("findExistUser",{username:username},function (data) {
let msg =$("#msg_username");
if(data.userExist==true)
{
/**
* 用户名存在
*/
msg.css("color","red");
}
else
{
/**
* 用户名不存在
*/
msg.css("color","green");
}
msg.html(data.msg);
},"json")
})
})
<form>
<label for="username">用户名</label><input type="text" id="username" name="username" >
<span id="msg_username"></span><br>
<label>
密码 <input type="password" name="username" >
</label><br>
<input type="submit" value="注册">
</form>
发送到findExistUser
这个控制器
代码如下
WebServlet("/findExistUser")
public class FindExistUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
/**
* 调用service层 判断用户是否存在
*/
//这里假设数据库中有tom这个数据
Map<String,Object>msg=new HashMap<>();
resp.setContentType("text/html;charset=utf-8");
if("tom".equals(username))
{
msg.put("userExist",true);
msg.put("msg","此用户名太受欢迎");
}
else
{
msg.put("userExist",false);
msg.put("msg","此用户名可用");
}
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.writeValue(resp.getWriter(),msg);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
这里有两个小问题 第一个防止乱码
resp.setContentType("text/html;charset=utf-8");
此外 还可以在这里把text/html这个mime-type换成json的application/json mime-type
这样的话就不用在 前端的js $.get()的最后一个参数里面输入接收的格式为json
了